Build a Secure Password Reset System with Twilio Verify in CakePHP
Time to read: 5 minutes
Build a Secure Password Reset System with Twilio Verify in CakePHP
Forgetting a password can be a frustrating experience for users and a security concern for developers. A secure password reset system ensures that only the rightful account owner can regain access. Specifically, instead of relying on emails, using phone numbers for password resets adds an extra layer of security and accessibility.
In this tutorial, we will build a secure password reset system in CakePHP using Twilio Verify backed by a MySQL database. You'll learn how to generate and send one-time verification codes to users' phone numbers, validate those codes, and securely update passwords. By the end of this guide, you'll have a robust password reset system that enhances both security and the user experience.
Prerequisites
To follow along with this tutorial, the following prerequisites are required:
- PHP 8.3 (8.4 is not fully supported, currently)
- Composer installed globally
- Access to a MySQL database
- Basic knowledge of or experience with CakePHP
- A Twilio account (free or paid). Create a new account if you are new to Twilio
- Your preferred text editor or IDE and web browser
Create a CakePHP project
To create a new CakePHP project using Composer, run the command below in your terminal.
When you see the prompt " Set Folder Permissions? (Default to Y) [Y, n]?", respond with " Y" to proceed with the project installation.
After that, run the command below to navigate to the project's working directory and start the application development server.
After the application server starts, open http://localhost:8765 in your browser to view the application's default welcome page, as shown in the screenshot below, to confirm that the base application is working.


Now, open the project code in your preferred code editor.
Set up the database
To connect your application to the MySQL database, open the project in your code editor. Then, navigate to the config folder and open the app_local.php file.
The database configuration is in the default
subsection of the Datasource
section. In this subsection, you will need to replace the values for the host
, username
, password
, and database
name with your database details.


Next, log in to your MySQL database server and create a new database named users
.
Then, create a database table named users
using CakePHP's migration features. First, run the command below to generate the migration file defining the table's properties.
After that, go to the config/Migrations folder, open the migration file that ends with _Users.php, and add the following code to the change()
function.
To complete the database migration, run the command provided below.
Install Twilio's PHP Helper Library
To simplify integrating Twilio's Verify API in your application, install Twilio's PHP Helper Library using the command below.
Store your Twilio credentials as environment variables
To connect to Twilio using the PHP Helper Library, you will need your Twilio Account SID and Auth Token. We'll store them as environment variables in a .env file to keep them out of the code, making them easier to manage. CakePHP does not support .env files out of the box, so you will need to configure the application to do so.
To set up the .env file, start by creating a copy of the config/.env.example file and renaming it to .env. You can do this by running the command below:
Next, open the .env file and add the following environment variables to the end of the file.
Now, you will need your Twilio Account SID and Auth Token. You can find these by logging into your Twilio Console dashboard. They are located in the Account Info panel, as illustrated in the screenshot below.


Substitute the first two placeholders in .env with your Twilio Account SID and Auth Token.
Set up a Verification Service
Next, you need to set up a Twilio Verification Service. This service provides common configurations for creating and verifying one-time passwords (OTPs). To do this, go to the Twilio Console and navigate to Explore Products > User Authentication & Identity > Verify.


Click on "Create new" and complete the form that appears. Enter a "Friendly name" for the service, and check the box labelled "Authorize the use of friendly name". Under the Verification channels section, enable SMS. After that, click "Continue," and then click "Continue" again in the next prompt.


Now, you will see the Service settings page for your new service. Copy the Service SID and paste it into .env in place of <twilio_verification_service_id>
.
Create a database entity and model
To generate the application model and entity files, run the command below.
The command will create a model file named UsersTable.php in the /src/Model/Table folder and an entity file named User.php in the /src/Model/Entity folder.
Create the controller
Next, let's create the application's controller. Execute the command below to generate a controller file named UsersController.php within the src/Controller directory.
Next, add the application logic to the controller by opening the UsersController.php file and replacing the existing code with the following.
Here is a breakdown of the above code:
The
register()
method validates the registration form and saves the user’s record. If successful, the user is redirected to the reset page. Otherwise, an error message is displayed.The
reset()
method checks if the provided phone number exists in the database. If found, an OTP is sent via Twilio, and the user is redirected to the verification page. Otherwise, an error message is displayed.The
verifyOtp()
method verifies the OTP entered by the user. If valid, the session is cleared, and the user is redirected to reset their password. Otherwise, an error message is displayed.The
resetpassword()
method updates the user’s password. If successful, the user is redirected to the login page. Otherwise, an error message is displayed.
Create the UI template files
For each method in the UsersController
you need to create a corresponding template that handles the page's content. To do that, navigate to the templates folder and create a new folder named Users. Inside the Users folder, create the following files:
register.php
reset.php
resetpassword.php
verify_otp.php
Next, add the following code to the register.php file:
Then, add the following code to the reset.php file:
Now, add the following code to the resetpassword.php file:
Finally, add the following code to the verify_otp.php file:
Add the route configuration
Now, let’s add routes for "register", "reset", "resetpassword", and "verify_otp" to the application routes. To do that, navigate to the config folder and open the routes.php file. Inside the file, locate $routes->scope()
and add the following code before $builder->fallbacks()
.
Test the application
Ensure the application is still running. Open http://localhost:8765/register in your browser to access the registration page and register a new user.


After successfully registering your account, you will be redirected to the reset password page where you will input the phone number used in registering.


Below is an image of the OTP received:


You will receive an OTP code via message on inputting your phone number. Enter the verification OTP received as shown in the screenshot below.


After the verification is complete, users can now input a new password successfully, as shown in the image below:


That's how to build a secure password reset system with Twilio Verify in CakePHP
In this tutorial, you learned how to build a secure password reset system in a CakePHP application using Twilio Verify. You implemented phone number verification for password reset requests, ensuring enhanced security by replacing traditional email-based resets with OTP authentication.
Isijola Jeremiah is a developer who specialises in enhancing user experience on both the backend and frontend. Contact him on LinkedIn.
Password icons created by kliwir art on Flaticon.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.