Laravel Jetstream includes login, registration, email verification... When a new user clicks on the Sign up button of an app, he or she usually gets a confirmation email with an activation link.After the click on the activation link, the user is authenticated for the app.
1. First Enable email verification on config/fortify.php
'features' => [
...
Features::emailVerification(),
...
],
2. Email setup on .env
Since our Laravel app will send a confirmation email, we need to set up the email configuration in the .env file. For email testing purposes, the dummy SMTP server provided by Mailtrap.io is the best option here. Besides, it’s a default SMTP driver in Laravel. All you need to do is sign up (a lifetime free trial is available) and add your credentials to .env, as follows:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<********> //Your Mailtrap username
MAIL_PASSWORD=<********> //Your Mailtrap password
MAIL_ENCRYPTION=tls
3. Set up email verification using the MustVerifyEmail
contract
The Must Verify Email contract is a feature that allows you to send email verification in Laravel by adding a few lines of code to the following files:
App/User.php:
Implement the MustVerifyEmail
contract in the User
model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
Add routes/web.php
Add such routes as email/verify
to the app:
Route::get('/', function () {
return view('welcome');
});
Route::get('/email/verify', function () {
return view('auth.verify-email');
})->middleware('auth')->name('verification.notice');
Now you can test the app.
And that’s what you’ll see in the Mailtrap Demo inbox:
Resources
Laravel Email Verification: Easy and Quick | Mailtrap
Laravel Jetstream basic setup with email verification in Laravel 8
Comments (0)