How to Create a Magic Link in Laravel with Twilio SMS
Time to read:
How to Create a Magic Link in Laravel with Twilio SMS
It’s best practice for users to use unique passwords for different websites. While this helps protect their accounts, it also increases the chances of users forgetting their passwords, which negatively impacts the user experience.
Magic links address this issue by allowing users to log in without remembering passwords or relying on the "Forgot Password" feature. Instead, they can simply request a magic login link, making it easier to access their accounts while maintaining robust security and improving the overall user experience.
In this tutorial, you'll learn how to create a magic link in Laravel with Twilio SMS. When a user enters their phone number, they’ll receive a one-time magic link via SMS. Clicking the link will log them into the application instantly.
Prerequisites
To follow along with this tutorial, you will need:
- PHP 8.3 or higher installed
- Composer installed globally
- A Twilio account with an active phone number capable of sending SMS
- Ngrok installed on your computer and an ngrok account
Create a new Laravel project
Let's get started by creating a new Laravel project using Composer. To do this, open your terminal, navigate to your working directory, and run the command below.
After the installation is complete, open the project folder in your preferred text editor or IDE.
Configure the database
Next, let’s define the database tables and their schema properties. To do this, you need to prepare the application for migration using the command below.
The command above will create a migration file inside the database/migrations folder. Navigate to the folder, open the migration file that ends with _register_users.php, and replace its code with the following to define the register_users table schema properties:
Save the changes, then run the command below to migrate the database.
Retrieve and store your Twilio credentials as environment variables
Next, let’s add your Twilio Account SID, Access Token, and phone number as environment variables. To do this, open the .env file and add the following code.
Then, to retrieve your Account SID, Access Token, and phone number, log in to your Twilio Console dashboard. In the Account Info section, you will find your Twilio credentials, as shown in the screenshot below.
Replace <twilio-account-sid>, <twilio-auth-token>, and <twilio-phone-number> in .env with the corresponding Twilio values.
Install Twilio's PHP Helper Library
To enable the application to easily interact with Twilio’s Programmable Messaging API, you need to install the Twilio PHP Helper Library using the following command:
Create a database model
Let’s create a database model class named RegisterUser to define how the application interacts with the underlying database table. To do this, run the command below.
The above command will generate a RegisterUsers.php file inside the app/Models directory. Navigate to the folder, open the file, and up it to match the following code.
Next, let’s add the RegisterUsers model to config/auth.php to handle authenticated user data. To do this, navigate to the config folder, open the auth.php file, locate the users subsection under the providers section, and replace its code with the following:
Create the registration controller
Now, to implement the user authentication logic for the application, you need to create a controller file. To do this, run the following command:
The command above will generate an AuthController.php file inside the app/Http/Controllers folder. Navigate to the folder and open AuthController.php.
Next, let’s create the application logic to allow users to register, log in with a magic link, and view their profile after successful authentication. To do this, replace the contents of the AuthController.php file with the following code:
In the code above:
- The
register()function handles user registration by validating the input fields, hashing the password, and saving the user data in the database - The
sendMagicLink()function generates a magic login link and sends it to the user's phone number via SMS using the Twilio Programmable Messaging API - The
magicLogin()function validates the magic link, logs the user in, and redirects them to the profile page - The
showProfile()function displays the user's profile page if they are logged in. If the user is not authenticated, they are redirected to the login page
Create the application user Interface
Let’s create the user interface for the registration, login, and profile pages. To do this, run the commands below:
The commands generate layout.blade.php, register.blade.php, login.blade.php, and profile.blade.php template files inside the resources/views/components directory.
Open the layout.blade.php file and replace its contents with the following:
Open the register.blade.php file and replace its contents with the following:
Open the login.blade.php file and replace its contents with the following.
Finally, open the profile.blade.php file and replace its contents with the following.
Configure the application's routes
Next, let’s configure the application routes. To do this, navigate to the routes folder, open the web.php file, and replace its contents with the following:
Start the application
Now, let’s start the application’s development server by running the command below:
Then, you need to make the application accessible over the internet using ngrok to ensure the magic link works properly. To do this, open another terminal tab or window and run the command below:
The command above will generate a forwarding URL, as shown in the screenshot below.
Lastly, open the .env file and replace the <ngrok-forwarding-url> placeholder with the generated forwarding URL.
Test the application
To test the application, open the generated forwarding URL in your browser and append " /register" to it. Then, register a new account, as shown in the screenshot below.
After successful registration, you will be directed to the login page, where you can log in with a magic link. On the login page, enter your registered phone number and click the Send Magic Link button, as shown in the screenshot below.
After requesting the magic link, you will receive an SMS on your phone containing the magic link, as shown in the screenshot below.
Click the magic link to log in to your account, as shown in the screenshot below.
That’s how to create a magic link in Laravel with Twilio SMS
In this tutorial, you learned how to create a secure magic link system to easily authenticate users in a Laravel application using Twilio's Programmable Messaging API. This approach simplifies the login process and enhances security by eliminating the need for traditional passwords.
Popoola Temitope is a mobile developer and a technical writer who loves writing about frontend technologies. He can be reached on LinkedIn .
Magic icons and Link icons created by Freepik 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.