Laravel 9 Custom Email Verification System

May 02, 2022

.

Admin

Laravel 9 Custom Email Verification System

Hello friends,

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

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

You are just following my steps and you will set up for email verification in laravel 9 project.

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Database Configuration

This step is to configure your database details in the .env file. So let's create a 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 9 by following command:

php artisan migrate

Step 3: Email Configuration

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

MAIL_USERNAME=youremail@gmail.com

MAIL_PASSWORD=yourpass

MAIL_ENCRYPTION=tls

Step 4: Install Auth

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

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

composer require laravel/ui

Now you can use a quick way to create a 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 the npm command because you can see a better layout of the login and register page.

Install NPM:

npm install

Run NPM:

npm run dev

Step 5: Add Model

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

];

}

Step 6: Add Route

routes/web.php

<?php

use App\Http\Controllers\HomeController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

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

Step 7: Add Controller

php artisan make:controller HomeController

app/Http/Controllers/HomeController.php

<?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');

}

}

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/home

It will help you...

#Laravel 9