Adding two-factor authentication to your application is the easiest way to increase security and trust in your product without unnecessarily burdening your users. This quickstart guides you through building a PHP, Laravel and AngularJS application that restricts access to a URL. Four Authy two-factor authentication channels are demoed: SMS, Voice, Soft Tokens and Push Notifications.
Ready to protect your toy app's users from nefarious balaclava wearing hackers? Dive in!
Once logged in, visit the Authy Console. Click on the red 'Create New Aplication' (or big red plus ('+') if you already created one) to create a new Authy application then name it something memorable.
You'll automatically be transported to the Settings page next. Click the eyeball icon to reveal your Production API Key.
Copy your Production API Key to a safe place, you will use it during application setup.
This Authy two-factor authentication quickstart demos two channels which require an installed Authy Client to test: Soft Tokens and Push Notifications. While SMS and Voice channels will work without the client, to try out all four authentication channels download and install Authy Client for Desktop or Mobile:
To complete the quickstart today we'll use PHP 7.0+, Composer, MySQL, and the Twilio PHP Helper Library. Let's walk through installation below. Skip ahead if you have already installed one.
When doing PHP web development, we strongly suggest using Composer for package management. This quickstart relies on Composer to install the Twilio PHP Helper library. You can find manual installation instructions on the PHP Helper Library page.
You'll need a database for storing Authy IDs when a user registers in this quickstart. For this demo, we built our user database on top of MySQL 5.x.
If you haven't yet installed MySQL, here are instructions for your platform:
When installed, start it. If you're using the default MySQL credentials (as below), create schema
account_security with an admin user
homestead and password
Clone our PHP repository locally, then enter the directory.
git clone firstname.lastname@example.org:TwilioDevEd/account-security-quickstart-php.git cd account-security-quickstart-php composer install
cp .env.example .env
Next, copy your Authy API Key from the Authy Dashboard and set the
API_KEY variable in your
Once you have added your API Key, you are ready to run! Launch the app with:
php artisan key:generate php artisan migrate php artisan serve --port 8081
You should get a message your new app is running!
With your phone (optionally with the Authy client installed) nearby, open a new browser tab and navigate to
Enter your information and invent a password, then hit 'Register'. Your information is passed to Twilio (you will be able to see your user immediately in the console), and the application is returned a
Now visit http://localhost:8081/login/index.html and login. You'll be presented with a happy screen:
If your phone has the Authy Client installed, you can immediately enter a Soft Token from the client to Verify. Additionally, you can try a Push Notification simply by pushing the labeled button.
If you do not have the Authy Client installed, the SMS and Voice channels will also work in providing a token. To try different channels, you can logout to start the process again.
And there you go, Authy two-factor authentication is on and your PHP/Laravel app is protected!
Now that you are keeping the hackers out of this demo app using Authy two-factor authentication, you can find all of the detailed descriptions for options and API calls in our Two-factor Authentication API Reference. If you're also building a registration flow, also check out our Phone Verification product and the Verify Quickstart which uses this codebase.
For additional guides and tutorials on account security and other products, in PHP and in our other languages, take a look at the Docs.