Rate this page:

Two-Factor Authentication with Authy, PHP and Laravel

For new development, we encourage you to use the Verify API instead of the Authy API. The Verify API is an evolution of the Authy API with continued support for SMS, voice, and email one-time passcodes, an improved developer experience and new features including:

  • Twilio helper libraries in JavaScript, Java, C#, Python, Ruby, PHP, and Golang
  • Access via the Twilio CLI
  • Improved Visibility and Insights
  • Push authentication SDK embeddable in your own application

You are currently viewing the Authy API. New features and development will be on the Verify API. Check out the FAQ for more information and the migrating to Verify guide to get started.

Adding Two-Factor Authentication (2FA) to your web application increases the security of your user's data by requiring something your user has to be present for step-up transactions, log-ins, and other sensitive actions. Multi-factor authentication determines the identity of a user by validating once by logging into the app, and then by validating their mobile device.

This PHP Laravel sample application is an example of a typical login flow using Two-Factor Authentication. To run this sample app yourself, download the code and follow the instructions on GitHub.

For the second factor, we will validate that the user has their mobile phone by either:

  • Sending them a OneTouch push notification to their mobile Authy Client app or
  • Sending them a token through their mobile Authy app or
  • Sending them a one-time token in a text message.

See how VMware uses Twilio Two-Factor Authentication to secure their enterprise mobility management solution.

Click here to start the tutorial!

Configuring Authy

If you haven't configured Authy already, now is the time to sign up for Authy. Create your first application naming it as you wish. After you create your application, your "production" API key will be visible on your dashboard

Once we have an Authy API key we can register it as an environment variable.


        Let's take a look at how we register a user with Authy.

        Register a user using Authy

        Register a User with Authy

        When a new user signs up for our website, we will call this route. This will save our new user to the database and will register the user with Authy.

        In order to set up your application, Authy only needs the user's email, phone number and country code. In order to do a two-factor authentication, we need to make sure we ask for this information at sign up.

        Once we register the User with Authy we get an authy id back. This is very important since it's how we will verify the identity of our User with Authy.

              See how to log a user in with OneTouch

              Log in with Authy OneTouch

              When a User attempts to log in to our website, we will ask them for a second form of identification. Let's take a look at Authy's OneTouch verification first.

              OneTouch works like this:

              • We attempt to send a User a OneTouch Approval Request.
              • If the User has OneTouch enabled we will get a success message back.
              • The User hits Approve in their Authy app.
              • Authy makes a POST request to our app with an approved status.
              • We log the User in.

                    Log in with Authy OneTouch

                    Send the OneTouch request

                    Send the OneTouch Request

                    When our User logs in we immediately attempt to verify their identity with OneTouch. We will fallback gracefully if they don't have a OneTouch device, but we won't know until we try.

                    Authy allows us to input details with our OneTouch request, including a message, a logo and so on. We could easily send any amount of details by appending details['some_detail']. You could imagine a scenario where we send a OneTouch request to approve a money transfer.

                    $params = array(
                      'message' => "Request to send money to Jarod's vault",
                      'details[From]' => "Jarod",
                      'details[Amount]' => "1,000,000",
                      'details[Currency]' => "Galleons",

                    Once we send the request we need to update our User's authy_status based on the response.

                          Configure OneTouch callback

                          Configure the OneTouch callback

                          In order for our app to know what the user did after we sent the OneTouch request, we need to register a callback endpoint with Authy.

                          Here in our callback, we look up the user using the authy_id sent with the Authy POST request. Ideally at this point we would probably use a websocket to let our client know that we received a response from Authy. However, for this version we're going to keep it simple and just update the authy_status on the User.


                                Update user status using Authy Callback


                                Let's take a look at the client-side code that will be handling this.

                                Handle authentication in the browser

                                Handle Two-Factor in the Browser

                                We've already taken a look at what's happening on the server side, so let's step in front of the cameras and see how our JavaScript is interacting with those server endpoints.

                                When we expect a OneTouch response, we will begin by polling /authy/status until we see an Authy status is not empty. Let's take a look at this controller and see what is happening.


                                      Handle Two-Factor in the Browser

                                      Accept or deny Authy status

                                      Finishing the 2FA Step

                                      If authy_status is approved the user will be redirected to the protected content, otherwise we'll show the login form with a message that indicates the request was denied.


                                            Redirect user to the correct page based based on authentication status


                                            That's it! We've just implemented two-factor authentication using three different methods and the latest in Authy technology.

                                            What's next?

                                            Where to next?

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

                                            Call Tracking

                                            Measure the effectiveness of different marketing campaigns with unique phone numbers.

                                            Server Notifications via SMS

                                            Faster than e-mail and less likely to get blocked, text messages are great for timely alerts and notifications. Learn how to send out SMS (and MMS) notifications to a list of server administrators.

                                            Did this help?

                                            Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Connect with us on Twitter and let us know what you build!

                                            Jarod Reyes Kelley Robinson Kat King Andrew Baker Paul Kamp
                                            Rate this page:

                                            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 by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.


                                                  Thank you for your feedback!

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

                                                  Sending your feedback...
                                                  🎉 Thank you for your feedback!
                                                  Something went wrong. Please try again.

                                                  Thanks for your feedback!

                                                  Refer us and get $10 in 3 simple steps!

                                                  Step 1

                                                  Get link

                                                  Get a free personal referral link here

                                                  Step 2

                                                  Give $10

                                                  Your user signs up and upgrade using link

                                                  Step 3

                                                  Get $10

                                                  1,250 free SMSes
                                                  OR 1,000 free voice mins
                                                  OR 12,000 chats
                                                  OR more