Hello Developers,
Sometimes we want to implement the default Laravel Email verification process. For that when we register a user the email sends through the system and when we click the link then the login page comes up.
The verification page:
After clicking the link, the login page occurs:
Let's configure the process.
Install Laravel Project In Your Environment
First, you need to go to your development workspace. My workspace is situated in D:/
drive. Open a command prompt and write the below code to install Laravel.
laravel new example-app
Database Configuration
After installing the project, open the project in your code editor. Now we have to configure the database credentials. As I am using the wamp server, I have to start the server and open the localhost/phpmyadmin
. In there I will create a database named example-app.
After that, add the credentials to the project .env
file
/.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=example-app
DB_USERNAME=[USERNAME]
DB_PASSWORD=[PASSWORD]
In this tutorial, we will use MailTrap. This service is used to test sending emails. Let's configure the mail credentials. Open the .env file and replace the below lines. This enables our application to deliver emails to the MailTrap inbox.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=[YOUR_USERNAME]
MAIL_PASSWORD=[YOUR_PASSWORD]
MAIL_ENCRYPTION=tls
Install Bootstrap Scaffolding
Any Laravel project will include Bootstrap and auth scaffolding. To initiate let's run the below command:
composer require laravel/ui
To make the laravel default bootstrap login design run the below command:
php artisan ui bootstrap --auth
Lastly, install the npm in the project. To do so, run the two commands one by one.
npm install
npm run dev
Also, run the mix command to get the design:
npm run watch
Finally, you can run the php artisan migrate
In the command prompt run the below command:
php artisan migrate
Sometimes you might get the below error:
If you get the error, simply add the following line in the [project_directory]/app/Providers/AppServiceProvider.php
/app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
As your table has already been generated. Now run the php artisan migrate:fresh
. Laravel application ships with a User Model. This model does not implement the Illuminate\Contracts\Auth\MustVerifyEmail
contract. Therefore, activate the MustVerifyEmail
interface.
/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;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Laravel also comes with a SendEmailVerificationNotification
listener. This event allows us to send a verification. Laravel itself manages its own default routing. Just add the routes in the web.php file.
/routes/web.php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify'=>true]);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Add the lines in the route. The web route is situated at /routes/web.php
/routes/web.php
<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify'=>true]);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Add the verified middleware in the home controller.
/app/Http/Controllers/HomeController.php
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(['auth','verified']);
}
Now run the below command:
php artisan serve
After registering, you will get an email verification page. When you verify the mail from the mail trap, you can go to the home page.
Hope this might help in the journey of development.
Read More: What is Accessor and Mutator in Laravel