laravel 8 email verification with laravel ui

In this tutorial you will see how to email verification with laravel ui pakage. laravel ui package Good for authentication scaffolding. it is simple ui , easy to install, if you are using bootstrap ui go for it.

In the previous section, we have to added login and registration to using ui package. Let's now see how to add email verification.

you can check our previous tutorial down below.

Laravel 8 Authentication with Laravel UI

Sending and Verifying Emails in Laravel 10 with Mailtrap.io


Laravel 8 Email Verification with Laravel UI

Step 1: Set up Configuring the SMTP Server

Step 2: Set up Laravel MustVerifyEmail Contract In Your Model

Step 3: Set the Email Verification Routes

Step 4: Set the Email Verification Middleware


Step 1: Set up Configuring the SMTP Server

You need to add the SMTP credentials in the .env file. in this section i use Mailtrap use can use any mail service. you need just

 register the mail trap account and go index page find Integrations select laravel section and add this credentials you .env file.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<YOUR_MAILTRAP_USERNAME>
MAIL_PASSWORD=<YOUR_MAILTRAP_PASSWORD>
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=<YOUR_Email_Address>
MAIL_FROM_NAME="${APP_NAME}"


Step 2: Set up Laravel MustVerifyEmail Contract In Your Model

Now, you need to implements MustVerifyEmail in User Model. Open the App/Models/User.php file and update as follows:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}


Step 3: Set the Email Verification Routes

Next,you need to add routes in app/routes/web.php file.

Auth::routes(['verify' => true]);


Step 4: Set the Email Verification Middleware

you can add verified Middleware HomeController or any controller which you want to not access any without verified user you can inject _construct method.

public function __construct()
    {
        $this->middleware(['auth', 'verified']);
    }

Personally i Prefer to add verified in routes in app/routes/web.php file. using group Middlweware

Route::group(['middleware' => ['auth', 'verified']], function () {
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
});

After  Registration you got this message

email

email


Next,you need to click Verify Email Address

email link

email link

Now you can access the home and other pages

verify

verify