How To Implement Email Verification In Laravel 10 Application

Here is a Laravel 10 email verification tutorial example.

  1. Install Laravel 10.
  2. Configure the mail driver in your .env file.
  3. Generate the authentication scaffolding using the php artisan make:auth command.
  4. Enable email verification in your config/auth.php file.
  5. Configure the email templates in your resources/views/emails directory.
  6. Modify the RegisterController to send the verification email.
  7. Create a route to handle the verification link.
  8. Test the email verification process.

Here are the detailed steps:

  1. Install Laravel 10 by running the following command:
composer create-project laravel/laravel myapp
  1. Configure the mail driver in your .env file. For example, if you are using Gmail, you would set the following values:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
  1. Generate the authentication scaffolding using the following command:
php artisan make:auth

This will create the following files:

  • app/Http/Controllers/Auth/RegisterController.php
  • resources/views/auth/register.blade.php
  • resources/views/auth/verify.blade.php
  1. Enable email verification in your config/auth.php file. Set the email_verification_enabled value to true.

PHP

'email_verification_enabled' => true,
  1. Configure the email templates in your resources/views/emails directory. The verify.blade.php template is used to send the verification email.
  2. Modify the RegisterController to send the verification email. In the register() method, add the following code:

PHP

$user->sendEmailVerificationNotification();
  1. Create a route to handle the verification link. The following route will redirect the user to the home page after they have verified their email address:

PHP

Route::get('/email/verify/{id}/{hash}', function (Request $request) {
    $request->user()->emailVerification->verify();
    return redirect('/home');
})->middleware(['auth', 'signed']);
  1. Test the email verification process by registering a new user and clicking on the verification link in the email.

I hope this tutorial helps you implement email verification in your Laravel 10 application.

SEE MORE POSTS LIKE THIS HERE

Leave a Reply