Laravel UI Login Register Email Verification in Laravel 8

Reading Time: 7 minutes
14,627 Views

After registration sending an email to registered user is very important if we are developing a secure application. Inside this article we will see the concept of Laravel UI login register email verification in laravel 8 tutorial.

Laravel UI is also one of the composer package which provides authentication scaffolding in laravel which includes Login, registration, forgot password, reset password routes.

There are few simple steps which is very easy to follow and to implement in laravel 8 application. We will see this tutorial from scratch. Email verification is laravel is very simple process to create.

  • Email verification in Laravel 8 with Jetstream & Livewire authentication, Click here.
  • Email verification in Laravel 8 with Breeze authentication, Click here.

While implementing email verification in laravel main focus is to write the routes, middleware. We will do these points –

  • Create Laravel 8 application with Laravel UI authentication (You can use any other, no issues)
  • We will see Model & Route configuration

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Laravel 8 Authentication with Laravel UI

We are using here Laravel UI to create application authentication section like login, registration, etc.

Install Laravel UI

We need to Install laravel/ui package for the authentication.

Open Laravel project to terminal and type the given command.

$ composer require laravel/ui

Once the laravel/ui package has been installed, you need to install the frontend scaffolding using the ui artisan command:

$ php artisan ui bootstrap --auth

After this command need to run the command

$ npm install && npm run dev

It will generate CSS and JS compiled files for authentication system.


Run Migration

Next, we need to run migration command to generate tables in database. Open terminal and run this artisan command.

$ php artisan migrate

SMTP Configuration with Laravel 8

To configure SMTP details, open up the file .env from application root. We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME="mygmail@gmail.com"
MAIL_PASSWORD="mypassword"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="mygmail@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"
  

Make sure you have enabled google security setting form your gmail. go to Google account and click on “Account”. Once you are on the “Account” page, click on “Security“. Scroll down to the bottom and you will find “Less secure app access” settings. Set as ON.

Click here to learn in detail about SMTP Configuration in Laravel 8.


Model Preparation

Open User.php from /app/Models.

Before getting started, verify that your App\Models\User model implements the Illuminate\Contracts\Auth\MustVerifyEmail contract.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    // ...
}

Model file is not ready to catch data in email verification process steps.

Next, we need routes.


Add Email Verification Routes

Laravel provides convenient built-in services for sending and verifying email verification requests.

Email verification routes will be of three types –

  • Route which sends email to user to verify.
  • Route which changes the status when user clicks on verify email.
  • Route which resend the email verification mail.

Open web.php from /routes folder. We should see the added line into web.php after installation of laravel/ui is

Auth::routes();

Next, we need to generate email verification routes as well. So for that, simply we need to update it as –

Auth::routes(['verify' => true]);

Add Protecting Layer To Routes

Next, we need to add layer of middleware which protects users from direct access to routes. Once we verify the email address then it allows to go and check routes.

Open web.php from /routes folder.

use App\Http\Controllers\HomeController;

//...

Route::get('/home', [HomeController::class, 'index'])->name('home')->middleware(["verified"]);

verified is in-built route middleware available in laravel application. To see, open Kernel.php from /app/Http folder.

Search for $routeMiddleware.

//...

protected $routeMiddleware = [
  
   //...
  
   'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL – http://127.0.0.1:8000/

We will get Login and Register Link at landing page.

When we click on Register, we should get a register page. We need to fill information and then we hit Register. It will register user into database but verification pending.

We will get an email verification page, but when we register user automatically it send an email to provided email address.

Open email address, we will get an email verification mail. Simply we need to click and verify our email.

Once user will be verified then each protected routes will be open to use.

We hope this article helped you to learn about Laravel UI Login Register Email Verification in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.