Laravel 9 Custom Email Verification System
May 02, 2022 . Admin
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 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 LaravelLet 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-appStep 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.
.envDB_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 migrateStep 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.
.envMAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=youremail@gmail.com MAIL_PASSWORD=yourpass MAIL_ENCRYPTION=tlsStep 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 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 the npm command because you can see a better layout of the login and register page.
Install NPM:npm install
Run NPM:
npm run devStep 5: Add in 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: Create 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: Create Controller 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'); } }
It will help you...