Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Account Verification with Authy, PHP and Laravel

Download the Code

Ready to implement user account verification in your application? Here's how it works at a high level:

  1. The users begin the registration process by entering their data, including a phone number, into a signup form.
  2. The authentication system sends a one-time password to the user's mobile phone to verify the possession of that phone number.
  3. The user enters the one-time password into a form before completing registration.
  4. The user opens a success page and receives an SMS indicating that their account has been created!

Building Blocks

To get this done, you'll be working with the following Twilio-powered APIs:

Authy REST API

  • Authy Docs: Find quick starts, documentation, and all about the helper libraries.

Twilio REST API

  • Messages Resource: We will use Twilio directly to send our user a confirmation message after they create an account.

All of this can be done in under a half an hour with the simplicity and power of Authy and Twilio. Let's get started!

Click here to move on to the next step!

The User Model

The User Model for this use-case is pretty straight-forward, and Laravel offers us some tools to make it even simpler. If you have already read through the 2FA tutorial, this one probably looks very similar.

Loading Code Sample...
      
      
      
      
      database/migrations/2014_10_12_000000_create_users_table.php

      Migration to create user table

      database/migrations/2014_10_12_000000_create_users_table.php

      Pretty simple user model, right? Next, we're going to visit the registration form on the client side.

      Take a look at the user registration form

      Registration Form

      When we create a new user, we ask for a name, e-mail address, and a password. To validate their account, we also ask them for a mobile number with a country code. We use Authy to send a one-time password via SMS to this phone number.

      It is now the controller's responsibility to verify that the user provides the necessary information to create a new user. If the user is created successfully, they will be logged into the system automatically.

      Loading Code Sample...
          
          
          
          
          app/Http/Controllers/UserController.php

          Create New Users

          app/Http/Controllers/UserController.php

          Now the user is logged in but not verified. In the next step, we'll learn how to configure Authy to integrate with our application.

          Configure your application to work with Authy

          Configuring Authy

          In .env.example we list configuration parameters for the application. These are pulled from system environment variables, which is a helpful way to access sensitive values (like API keys). Using environment variables prevents us from accidentally checking them into source control. We also use our Laravel configuration file to load the key and inject Authy\AuthyApi into the application using a service provider.

          Now we need our Authy production key (sign up for Authy here). When you create an Authy application, the production key is found on the dashboard.

          Authy dashboard

          Loading Code Sample...
              
              
              
              
              app/Providers/AuthyApiProvider.php

              Register the service provider for the Authy API

              app/Providers/AuthyApiProvider.php

              Now that we've learned how to configure Authy, we need to jump over to the UserController to configure the Authy client and create an instance method to send a one-time password.

              Learn how to send a one-time token to a new user

              Sending a Token on Account Creation

              Once the user has an authy_id we can actually send a verification code to that user's mobile phone.

              When our user is created successfully via the form we implemented, we send a token to the user's mobile phone to verify their account in our controller. Once the code is sent, we redirect to another page where the user can enter the token they received, completing the verification process.

              Loading Code Sample...
                  
                  
                  
                  
                  app/Http/Controllers/UserController.php

                  Send verification token via SMS on user creation

                  app/Http/Controllers/UserController.php

                  Next, we'll take a look at verifying the code the user provides us.

                  Verify your user's code

                  Verifying the Code

                  This controller function handles the submission form. It needs to:

                  • Get the current user.
                  • Verify the code that was entered by the user.
                  • If the code entered was valid, flip a boolean flag on the user model to indicate the account was verified.

                  The Authy PHP client provides us with a verifyToken() method that allows us to pass a user id and a token. In this case, we just need to check that the API request was successful and if so, set $user->verified to true.

                  Loading Code Sample...
                      
                      
                      
                      
                      app/Http/Controllers/UserController.php

                      Verify a user-submitted token

                      app/Http/Controllers/UserController.php

                      That's all for token verification! However, our verification form wouldn't be very usable if there wasn't a way to resend a verification code if the message didn't arrive at the end user's handset.

                      See how to resend verification codes

                      Re-Rending the Code

                      Since the form for re-sending the code is one line, we're going to skip that for this tutorial. Let's just look at the controller function.

                      Loading Code Sample...
                          
                          
                          
                          
                          app/Http/Controllers/UserController.php

                          Re-send a verification code when initial SMS fails

                          app/Http/Controllers/UserController.php

                          This controller loads the $user associated with the request and then uses the same Authy API method we used earlier to resend the code.

                          To wrap things up, let's implement the last step. We need to confirm that the user's account has been verified with a success page and a text message.

                          Let the user know that they have been verified

                          Show User Details Page

                          This simple blade template displays a user name and let's them know they've been verified.

                          Loading Code Sample...
                              
                              
                              
                              
                              resources/views/showUser.blade.php

                              Render user information with a blade template

                              resources/views/showUser.blade.php

                              This should suffice for confirmation in the browser that the user has been verified. Let's see how we might send that text message next.

                              Send a confirmation SMS!

                              Sending the Confirmation Message

                              We create a single instance of the Twilio REST API helper, called $client in this example.

                              Then all we need to do to send an SMS to the user's phone is use messages->create() method. Notice that we are using the user's fullNumber() to make sure we support international numbers. The fullNumber() method in the User model simply returns a combination of the country_code and phone_number that the user provided upon registration.

                              Loading Code Sample...
                                  
                                  
                                  
                                  
                                  app/Http/Controllers/UserController.php

                                  Send a confirmation message via SMS

                                  app/Http/Controllers/UserController.php

                                  We've just implemented account verification so your users can confirm their phone number. Where can we take it from here?

                                  What's next?

                                  Where to Next?

                                  If you're a PHP developer working with Twilio, you might want to check out these other tutorials:

                                  Click-To-Call

                                  Put a button on your web page that connects visitors to live support or sales people via telephone.

                                  Automated Survey

                                  Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.

                                  Did this help?

                                  Thanks for checking out this tutorial! 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've built!

                                  Kat King

                                  Need some help?

                                  We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                                  Loading Code Sample...