In this tutorial you will learn about the Laravel 8 Custom Email Verification System and its application with practical example.
In this Laravel 8 Custom Email Verification System Tutorial I will show you how to implement custom email verification for newly registered users in laravel. In this tutorial you will learn to enable custom email verification for account activation in laravel. Laravel comes with built-in email verification system for newly registered user. Using this, newly registered user will get an email with an account activation link. This activation link is used for account verification. When activation link is clicked, this will make user account verified and active for the application. Once user account is activated then it will be allowed to access protected routes; which can be accessible only by verified accounts.
Laravel 8 Custom Email Verification System
In this step by step tutorial, we’ll show you how to setup custom email verification system for newly registered user in laravel. Please follow the instruction given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Configuration Database and Email
- Step 3 – Install Laravel UI
- Step 4 – Install Bootstrap Auth Scaffolding
- Step 5 – Install Npm Packages
- Step 6 – Run PHP artisan Migrate
- Step 7 – Configure Model, Route And Controller
- Step 8 – Run Development Server
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel Demo |
Step 2 – Configuration Database and Email
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Now we will setup SMTP credential in .env to send emails. We are using mailtrap SMTP credential in this example.
1 2 3 4 5 6 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=your_mailtrap_user_name MAIL_PASSWORD=your_mailtrap_user_password MAIL_ENCRYPTION=tls |
Step 3 – Install Laravel UI
In this step we will install laravel ui package using following command:
1 |
composer require laravel/ui |
Step 4 – Install Bootstrap Auth Scaffolding
Now we need to enable bootstrap authentication scaffolding in laravel.
1 |
php artisan ui bootstrap --auth |
Step 5 – Install Npm Packages
Now we will instal npm dependencies using following command:
1 |
npm install |
Then type the following command on cmd to run npm:
1 |
npm run dev |
Step 6 – Run php artisan Migrate
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 7 – Configure Model, Route And Controller
Visit app/Models and open User.php file and add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?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', ]; } |
Then visit routes directory and open web.php and add the following rotues into it:
1 |
Auth::routes([‘verify’ => true]); Route::get(‘/home’, [App\Http\Controllers\HomeController::class, ‘index’])->name(‘home’); |
Aftert that, visit app/Http/Controllers and open HomeController.php and add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?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'); } } |
Step 8 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/ |