Step 1: Send the code Step 2: Receive SMS code Step 3: Valid match Step 4: Invalid match Wrap up Send a POST request to /verify-code with the previous REAL PHONE NUMBER and the code you received in the SMS.Wait until you receive the SMS on the phone.Send a POST request to /send-code with a REAL PHONE NUMBER inside the request body.We will use an HTTP client like Postman to test the application here is the process: Inside the src folder, create a file called redis-client.ts and add the code below: import Redis from 'ioredis' Ĭonst redisUrl = _URL || 'redis://127.0.0.1:6379' Įxport ) We will use ioredis: yarn add ioredis Create the Redis client We now need to install a Redis client for Node.js that help to interact with Redis from our Node.js code. I will use Docker here, so run the command below to start a container: docker run -name node-otp-redis -d redisīy default, Redis runs on port 6379, and Docker maps it to the same port on your computer. To quickest way to have Redis installed on your computer is to run a Docker container or use a Redis cloud provider like Upstash or RedisLabs. Basically, we will store the phone number and the random digit with a Time To Live (TTL) of 10 minutes. Since the validation must be done in a limited time (10 minutes, for example), we will use Redis, an in-memory key-value store database that makes it possible to store ephemeral data. Send an SMS with the random number to the user's phone number.Store the random code generated with the phone number for a further verification.Extra the phone number from the request's body.Sending the code to a phone number required many steps: We can now use the client to interact with the API Send an SMS with the code Inside the folder src, create a file called client.ts and add the code below: import twilio from 'twilio' Ĭonst accountSid = _ACCOUNT_SID Ĭonst authToken = _AUTH_TOKEN Ĭonst twilioClient = twilio(accountSid, authToken) env file to add this value your file will look like this: HOST= PORT=4500 The credentials we need here are the ACCOUNT_SID and the AUTH_TOKEN. To interact with the SMS, let's install the package for Node.js: yarn add twilio Create a client SDKĪ client SDK is an instance of the SDK with the credentials configured, so the API will know who is performing the request. There is an SDK for each language supported by Twillio services, to interact with their API from your own source. env.example you can duplicate to create your local. This file is excluded from the Git version, but there is a template called. When you launch your application, all the variables located in the file '.env' are injected into the node process. The project comes with the support of environment variables, thanks to dotenv. Navigate to the URL to verify it works as expected. Let's clone it and launch it locally: git clone -b express node-otp-twilio The application will expose two endpoints to respectively send a unique code by SMS and verify the code received by SMS. I created a boilerplate to quickly start a Node.js project with Express Framework. We will need it in our Node.js application. Click on the button "Provision and add this number".Ĭopy this number and keep it somewhere because we will need it later alternatively, you can go to the menu "Phone Numbers" in the left sidebar to find it.įrom this page, copy the account SID and the Auth Token. Next, click on the link labeled "Get set up".Įnter the Messaging service name and click on the button "Create Messaging Service"Ī phone number will be generated for you and displayed in the next step. On the dashboard, go to the left sidebar and click on the menu "Messaging" => "Overview". There is no need to follow the steps shown on the page since we will see them in this tutorial. You will reach the dashboard, which is the end of this part. Choose the same answer as in the picture below. Once your phone number is verified, the last step is to provide some information about your goal for using Twilio. When you enter your phone number and click on the button "Verify", you will receive an SMS with a code to enter on the next page. Once your email is confirmed, you will need to provide a phone number for verification. You will receive an email in your inbox to verify your account. Fill the inputs with your information and click on the button "Start your free trial".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |