Laravel 8 Custom Email Verification System

Laravel 8 , Laravel

Nicesnippets

26020

28-04-2021


Laravel 8 Custom Email Verification System

Hello friends,

Today i will discuss the custom email verification system in laravel 8 .You can understand a concept of laravel 8 auth verify email.laravel 8 email verification tutorial step by step. I explained simply step by step laravel 8 auth verify email. Let's see bellow example laravel 8 authentication email verification.

laravel old version we are doing email verification process manually, but in laravel 8 they provide in build email verification setup for new registered users to must have to verify his email before proceed. You just need to make some basic setup with need to use middleware, routes and mail configuration.

You are just folling to the my steps and you will set up for email verification in laravel 8 project.

Step 1: Create laravel 8 project


First step to create a fresh laravel 8 project in using bellow command.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Configuration your database

This step to configure your database details in .env file.So let's create username, password etc. So let's add.

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=Enter_Your_Database_Name

DB_USERNAME=Enter_Your_Database_Username

DB_PASSWORD=Enter_Your_Database_Password

After added database configuration then run the default migrations of laravel by following command:

php artisan migrate

Step 3: Email Configuration for .env

You need to add email configuration in .env file.Because We are sending email after user registration so we need to add email smtp details for send email.

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

[email protected]

MAIL_PASSWORD=yourpass

MAIL_ENCRYPTION=tls

Step 4: Install Auth

You can follow my few step to install auth in your laravel 8 project.

First you need to laravel/ui package in your laravel project

composer require laravel/ui

Now you can use quick way to create registration, login and forgot password with routes by auth command, So simply run bellow command to create

php artisan ui bootstrap --auth

Then later you need to run npm command, because you can see better layout of login and register page.

Install NPM:

npm install

Run NPM:

npm run dev

Step 5: Setup Email Verification in laravel project

This is a last step to the verification to the email setup so easily you have to add email verification class implementation in user model and use middleware for protection.

app/Models/User.php

<?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',

];

}

routes/web.php

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

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

app/Http/Controllers/HomeController.php

last change to the HomeController.php file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware(['auth','verified']);

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

return view('home');

}

}

So you are easily to learn in how to setup in custom email in laravel 8 project.

It will help you...