Here is a Laravel 10 email verification tutorial example.
- Install Laravel 10.
- Configure the mail driver in your
.env
file. - Generate the authentication scaffolding using the
php artisan make:auth
command. - Enable email verification in your
config/auth.php
file. - Configure the email templates in your
resources/views/emails
directory. - Modify the
RegisterController
to send the verification email. - Create a route to handle the verification link.
- Test the email verification process.
Here are the detailed steps:
- Install Laravel 10 by running the following command:
composer create-project laravel/laravel myapp
- 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
- 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
- Enable email verification in your
config/auth.php
file. Set theemail_verification_enabled
value totrue
.
PHP
'email_verification_enabled' => true,
- Configure the email templates in your
resources/views/emails
directory. Theverify.blade.php
template is used to send the verification email. - Modify the
RegisterController
to send the verification email. In theregister()
method, add the following code:
PHP
$user->sendEmailVerificationNotification();
- 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']);
- 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.