Hello Laravel learners! You heard about the latest release of Laravel? I think it has come several days ago. As you know, Laravel is the greatest PHP Framework available in web development field. This new release also has pretty interesting features. Among them, Email Authentication is selected to demonstrate you today. What kind of authentication is this? When you register to a site, you may have already seen they send a verification link to your email. You are allowed to login after you click on that link only. This is the common verification process. Did you know this process can be implemented using Laravel within 5 minutes? That's the power of Laravel! Let's start!
This is a demo of what we are doing today! Watch it :D (please change the quality to 720p)
This is a demo of what we are doing today! Watch it :D (please change the quality to 720p)
Create new laravel project
composer create-project laravel/laravel authentication
Configure database
Open the project in a text editor and edit database configuration files. I use MYSQL database for this application..env file
config/database.php
Before database migration, we have something to do to avoid migration errors... In your project folder, open Http/Providers/AppServiceProvider.php file and replace its code with this.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Now we are ready to migrate. Migrate using this command. Then our database laravel_email will have 3 tables called users, migrations and password_resets.
php artisan migrate
From Laravel 5.7 onward, the users table has an additional column called email_verified_at. It will be added automatically.
Now we are going to implement the user authentication!
Step 1 - Modify the User Model
By default, Laravel makes a model called User during the installation. We have to use this for users. Open the User.php included in app folder.
You can see this file contains User class! It's totally OOP.. The class has been extended by Authenticatable class by default. Then we inherit super class methods to user class. Then what we have to do?
Implement the User class with MustVerifyEmail interface.
Then import the MustVerifyEmail interface into User.
That's all guys for this step! This is the final code in User class.
Step 2 - Setup an Email platform
For easy developments, mostly used email platform is Mailtrap.
Go to this site and register yourself. Just use a Gmail account and log in. After you log in, you will be provided some credentials.. Look at them! There you will have a username and password with a random string. We need both of them for our mail driver configurations.
Open .env file in your Laravel project. Place the credentials in the section called MAIL_DRIVER.
Step 3 - Create login and validate routes with verification
As you know, laravel gives us a fully completed login and registration system with one single command. Open a terminal and hit php artisan make:auth
This will create all the stuff needed for a login and registration system. Run the project and see the home page.. On the top right corner you have LOGIN and REGISTER links. Now we can register to the application. But if we register now, the usual register process will be implemented without email verification.
We have to SETUP A MIDDLEWARE for AUTH ROUTES created by Auth Process.
Open routes/web.php file and modify the Auth routes and Home route by adding the middleware called verified. Do it as I did here.
Now we are ready to test the application! Easy no?
Register to the application with the email you registered in the Mailtrap email platform!
It will take some seconds after you entered the details. Because the application sends an email to your Mailtrap account. After some moment, you will display a web page like this.
Then you will get an email into Mailtrap Inbox like this.
Click on the verify button and then you will be verified. You can login easily then.
Additional Step
Usually after you register to a site, there's a profile page. If you want to restrict this page until the user is verified, you can do this to profile route. Simply add the verified middeware at the end.
You can do this to any route you want.
This is the end of the email verification process guys!
Is it difficult? T don't think so. Laravel do all the things for us. We have only to setup the environment for Laravel. This is the awesomeness of Laravel. Always it reduces the coding effort and provide and elegant application to the end user. So, if you are interested in this framework, Start practicing! I'm sure you will love it!
My Laravel Article series from start :
Try this and let me know if you have something to be clarified. Until I come with another Laravel article, Good Bye guys!
Laravel Email Authentication with 3 Steps
Reviewed by DAL
on
December 01, 2018
Rating:
No comments: