Laravel Email Verification Tutorial With Example Step By Step.
Email verification is useful feature for providing more security to new users of your web site.
This feature ensures us that the user has given us the correct email address and he is the owner of the email id.
Laravel gives rich environment to implement email verification with laravel.
It provides some easy to go techniques which simplifies the authentication process for email verification.
Checkout the below youtube video to show the final outcome.
Move 1. Create a new project
First of all, fire the below command in your terminal.
laravel new laramailveri
Above command will make a new project with laravel 5.8 version. Name of the project will be “laramailveri“.
Move 2. Start Authentication
Use the following command in terminal.
php artisan make:auth
This command will enable the authentication scaffolding in our app.
After the execution of this command, you will be able to use simple register and login in your web site. (However, we still need to connect our project with database, we will do it in next step.)
System generates some necessary files for authentication using this command. Let us see these files.
Navigate to Http->Controller->Auth directory.
RegisterController.php, LoginController.php, ForgotPasswordController.php etc. files are there inside Auth folder.
These are programming logic related files.
To see view blade files navigate to resources->views->auth directory.
Inside auth folder, you will find some blade files like login.blade.php, register.blade.php etc.
You can change the look and feel of registration as well login page using these blade files.
Move 3. Database Task
Now let us connect our laravel app with MySQL database.
I am using sequel pro as an administration tool for MySQL database. If you are using other tool like phpMyAdmin or any other than don’t worry, the trick is the same for all of them.
Just create a new database in your tool and give it a name like “laraMailVeri“
Now come to your project and open .env file.
You need to edit below lines in this .env file.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laraMailVeri DB_USERNAME=root DB_PASSWORD=
My username is root and password is nothing so I have leave it as an empty.
Configure above lines as per your machine requirements.
After this, your laravel project has connected with MySQL database.
Now, trigger the following command in terminal.
php artisan migrate
Above command will create some tables in our MySQL database.
Move 4. Updating user.php
Navigate to app->User.php file.
In this file, a class named “User” is there. By default, this class do not implements anything.
We need to implement MustVerifyEmail to this class manually.
After this, source code for User.php looks like the below
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { use 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', ]; }
We just need to add implementation of MustVerifyEmail , no need to change other things.
Move 5. Writing Some Routes
Now let us add some routes in web.php file.
Navigate to routes->web.php file.
Here, you may find something like ” Auth::routes(); “
Replace it with the below line
Auth::routes(['verify' => true]);
Now there is also another line like ” Route::get(‘/home’, ‘HomeController@index’)->name(‘home’) ”
Replace this line with the following
Route::get('/home', 'HomeController@index')->name('home')->middleware('verified');
So last source lines for web.php file looks like the below
<?php /* |-------------------------------------------------------------------------- | 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', 'HomeController@index')->name('home')->middleware('verified');
Move 6. Final Demo Task
Now we need to have some email address tool which allows us to send verification mail.
I am using mailtrap for this.
So first of all, sign in to your mail trap account : https://mailtrap.io
On the dashboard, you will see some configuration details like username, password, port etc.
We need to connect our project using these details. For this, open your .env file and see the below lines.
MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=116ff5545b3a68 MAIL_PASSWORD=1e27cb61c6ca56 MAIL_ENCRYPTION=null
You just need to change above details as per your dashboard of mail trap.
Now all the tasks are done. Just run “php artisan serve” command from terminal and see that your laravel project is doing really well with verification mail.