Ready to implement user account verification in your application? Here's how it works at a high level:
- The user begins the registration process by entering their data, including a phone number, into a signup form.
- The authentication system sends a one-time password to the user's mobile phone to verify the phone number.
- The user enters the one-time password into a form to complete registration.
- The user sees a success page and receives an SMS indicating that their account has been created.
To get this done, you'll be working with the following Twilio-powered APIs:
Authy REST API
- Users Resource: You will need to create Authy users to send and verify one-time passwords.
- SMS Resource: We will ask Authy to send one-time passwords to our user via SMS.
- Verify Resource: Used to verify tokens entered by the user in our web form during registration.
Twilio REST API
- Messages Resource: We will use Twilio directly to send our user a confirmation message after they create an account.
Let's get started!
Our first order of business is to create a model object for a user of our application. We will borrow a lot of the code from the
User model in the 2FA tutorial that uses Authy as well. This application uses MongoDB for persistence, but in our code we will primarily interface with Mongoose, a higher-level object modeling tool which is backed by MongoDB.
You'll notice an
authyId property on the model - this is required to support integration with the Authy API. We won't use this property right away but we'll need it later for the Authy integration.
One of the properties on the
User model is the password. It is not in scope for this tutorial, but take note: you'll probably want it later for logging in a returning user.
Now that you've created your user model, let's check out the form template for creating a user.
When we create a new user, we ask for a name, e-mail address, password and mobile number including country code. In order to validate the user account we use Authy to send a one-time password via SMS to this phone number.
Now the user is logged in but not verified. In the next steps we'll learn how to verify the user using Authy.
config.js, we list configuration parameters for the application. Most are pulled in from system environment variables, which is a helpful way to access sensitive values (like API keys). This prevents us from accidentally checking them in to source control.
Now, we need our Authy production key (sign up for Authy here). Once you create an Authy application, the production key is found on the dashboard:
Next, we need to jump over to the
User model to configure the Authy client and create an instance method to send a one-time password.
When it comes time to actually send the user a verification code, we do that in a User model function.
Before sending the code, an Authy user needs to exist and correlate to our
User model in the database. If the
authyId for our user instance hasn't been set, we use the Authy API client to create an associated Authy user and store that ID.
Once the user has an
authyId, we can actually send a verification code to that user's mobile phone using the Authy API client.
After the user receives the verification code, they will pass it to the application using this form.
Let's check out the controller that handles the form.
This controller function handles the form's submission. It's a little longer than the others, but it has a lot to do. It needs to:
- Load a
Usermodel for the current verification request.
- Use an instance function on the model object to verify the code that was entered by the user.
- If the code entered was valid, it will flip a boolean flag on the user model to indicate the account was verified.
Take a look at the
User model to see the instance method that handles verifying the code with Authy.
Now let's see how we can use Authy to actually verify the code.
This is a great start, but what if your code never reaches the end user's handset? Authy can help us to re-send a missing code.
This controller function loads the
User model associated with the request and then uses the same instance function we defined earlier to resend the code.
To wrap things up, let's let the user know that their account has been verified via a success page and an SMS to their device.
This controller function renders a Jade template that contains the user's full name, and indicates whether or not they are verified by checking the user's
This should suffice for confirmation in the browser that the user has been verified. Let's see how we might send a confirmation via text message.
Congratulations! You now have the power to register and verify users with Authy and Twilio SMS. Where can we take it from here?
If you're a Node developer working with Twilio, you might want to check out these other tutorials:
Put a button on your web page that connects visitors to live support or salespeople via telephone.
Instantly collect structured data from your users with a survey conducted over a call or SMS text messages.
Thanks for checking this tutorial out! If you have any feedback to share with us, we'd love to hear it. Reach out to us on Twitter and let us know what you build!