Laravel 9 - UI Auth email verification

Touseef Afridi
27 Aug 24

Laravel 9 - UI Auth email verification

In this tutorial, we will discuss about the UI Auth email verification process in Laravel 9. Email verification confirms a user's identity, ensuring they provide a valid email address and helping to prevent fake or unauthorized accounts.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

In this guide, we’ll walk through setting up email verification in a fresh Laravel project. We’ll start by creating a new Laravel project using either the Laravel installer or Composer. After that, we’ll set up a database and migrate the required tables. We’ll install Laravel UI and set up authentication scaffolding, including Vue.js for the frontend. Then, we’ll configure the application to handle email verification by modifying the web.php and User model. We’ll also integrate Mailtrap for email testing and update the .env file with the necessary credentials. Finally, we’ll test the email verification process by registering a user and verifying their email address.

Step # 1 : Create Fresh Laravel Project.

Two commands to create fresh Laravel project
If you have the Laravel Installer installed globally, you can quickly create a new Laravel project named verify with the following command.
laravel new verify
Or use
If the Laravel Installer is not installed, you can use Composer to create a new Laravel project named verify with the following command.
composer create-project laravel/laravel --prefer-dist verify

Step # 2 : Create a database.

Create a database named verify using a database management tool and update the .env file in your Laravel project to connect to it.

Step # 3 : Migrate the tables.

Run the following command to migrate the necessary database tables.
php artisan migrate

Step # 4 : Let's install Laravel UI and set up authentication scaffolding.

Run the following commands to install the Laravel UI package, generate authentication views and routes, and install frontend dependencies.
composer require laravel/ui
php artisan ui vue --auth
npm install && npm run dev
Open another instance of Git Bash or your preferred command-line interface since npm install && npm run dev will run the Vite server, and you’ll need access to the project in another CLI instance.

Step # 5 : Access web.php and set email verify to true.

In web.php, enable email verification by setting verify to true in Auth routes.
Auth::routes(['verify' => true]);

Step # 6 : Modify the User Model for Email Verification.

In the User model, implement MustVerifyEmail to enable email verification functionality.
class User extends Authenticatable
To
class User extends Authenticatable implements MustVerifyEmail

Step # 7 : Update HomeController Constructor for Middleware.

In the HomeController, update the constructor to include the auth and verified middleware to ensure users are authenticated and have verified their email before accessing the home page.
public function __construct(){
$this->middleware(['auth', 'verified']);
}

Step # 8 : Create Mailtrap Account for Testing.

Create a Mailtrap account by visiting (Link : https://mailtrap.io/). After registering, create a project specifically for testing purposes. Once the project is created, you can access it to find all the necessary details that will be used in Step #9.


Step # 9 : Update .env File with Mailtrap Credentials.

Access the .env file in your project and replace the following details with your Mailtrap credentials for testing purposes.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME="Mailtrap username"
MAIL_PASSWORD="Mailtrap password"
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="your email address"
MAIL_FROM_NAME="${APP_NAME}"

Step # 10 : It's time to test.

Register a new user in your application. If everything is set up correctly, you will receive a verification email in your Mailtrap inbox. Access the email in Mailtrap and click on the Verify Email Address link to complete the email verification process.


Conclusion

This guide simplifies the process of adding email verification to a Laravel project. By following these steps, you can quickly implement and test email verification functionality using Mailtrap, ensuring your app has a secure registration process for new users.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering