How To Implement Email Verification In Laravel

Hello Geek, Below is an example tutorial on implementing email verification in Laravel, covering the necessary steps and providing explanations along the way. This tutorial assumes you have basic knowledge of Laravel and its concepts.

Implementing Email Verification in Laravel

Introduction:

Email verification is a crucial feature for web applications that require user registration and authentication. It ensures that the users provide a valid email address and helps prevent spam and fake accounts. In this tutorial, we will learn how to implement email verification in a Laravel application. We will cover the entire process, including user registration, sending verification emails, and verifying user accounts.

Prerequisites:

To follow along with this tutorial, you should have the following:

  1. A working Laravel installation (version 5.7 or above).
  2. A database connection configured in Laravel.
  3. Basic knowledge of Laravel’s authentication system.

Step 1: Set Up a New Laravel Project
Let’s start by creating a new Laravel project using the Laravel Installer. Open your terminal and run the following command:

laravel new email-verification

Step 2: Configure the Database
Next, navigate to the project’s root directory and open the .env file. Update the database connection details according to your setup. Make sure to provide valid credentials for your database.

Step 3: Set Up User Authentication
Laravel provides a built-in authentication system that we can leverage for user registration and authentication. Let’s scaffold the authentication views and routes by running the following command:

php artisan make:auth

This command will generate the necessary views and routes for user authentication.

Step 4: Generate the User Migration
We need a users table in the database to store user information. Run the following command to generate the migration file:

php artisan make:migration create_users_table --create=users

Open the generated migration file located in database/migrations and add the necessary columns for the users table. For example, we can include columns like name, email, password, and email_verified_at. Save the changes and run the migration:

php artisan migrate

Step 5: Update User Model
Open the User model located in app/Models and make sure it extends the Illuminate\Foundation\Auth\User class. This class provides the necessary methods for user authentication.

Next, include the Illuminate\Contracts\Auth\MustVerifyEmail contract in the User model. Add the following line at the top of the file:

use Illuminate\Contracts\Auth\MustVerifyEmail;

Then, update the class declaration to implement the MustVerifyEmail contract:

class User extends Authenticatable implements MustVerifyEmail
{
    // ...
}

Step 6: Update Registration Controller
Open the RegisterController located in app/Http/Controllers/Auth and include the Illuminate\Foundation\Auth\RegistersUsers trait. This trait contains the registration methods.

Add the MustVerifyEmail trait to the RegisterController class:

use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Auth\MustVerifyEmail;

Also, update the RegisterController class declaration to include the MustVerifyEmail trait:

class RegisterController extends Controller implements MustVerifyEmail
{
    // ...
}

Step 7: Update Email Verification Notification
By default, Laravel sends a basic email verification notification. However, we can customize it to match our application’s needs.

Create a new notification using the following command:

php artisan make:notification VerifyEmailNotification

This command generates a new notification class in the app/Notifications directory. Open the VerifyEmailNotification class and modify the `toMail

` method to define the email content and format.

Step 8: Update Email Verification Routes
Open the web.php file located in routes directory. Uncomment the Auth::routes() method call to enable the authentication routes.

Next, wrap the authentication routes inside the Route::middleware method, passing the ['verified'] middleware. This middleware ensures that only verified users can access the authenticated routes.

Route::middleware(['auth', 'verified'])->group(function () {
    // Authenticated routes
});

Step 9: Test the Registration and Email Verification Flow
At this point, we have implemented the necessary changes to enable email verification. Let’s test the flow by registering a new user.

Start the Laravel development server by running the following command:

php artisan serve

Open your web browser and navigate to http://localhost:8000/register. Fill in the registration form and submit it.

Check your email inbox for the verification email. Open the email and click on the verification link.

Once the email is verified, you will be redirected to the application’s home page.

Conclusion:

In this tutorial, we learned how to implement email verification in a Laravel application. We covered the entire process, including user registration, sending verification emails, and verifying user accounts. By following these steps, you can enhance the security and reliability of your Laravel application.

Feel free to explore more advanced topics, such as customizing email templates, adding email verification to existing users, or handling email verification failures. Laravel provides extensive documentation that can assist you in these endeavors.

Remember to always consider best practices for user authentication and email handling when implementing these features in a production environment.

All the best nerd!

SEE OUR PREVIOUS POST

Leave a Reply