Latest Post

6/recent/ticker-posts

Laravel 8 Authentication Tutorial example

Hi Dev,

In this blog, I will learn you how to implement authentication in your Laravel 8 project with example Alright, let’s dive into the steps.

Laravel 8 provide septate composer package for engendering auth scaffold in laravel 8 application. Whenever you require to engender auth in laravel 8 then you must have to install laravel/ui package in laravel 8.

Next, Utilizing laravel/ui you can engender simple view with auth as same you did afore you do. But you have to utilize vue js or react js with auth view in laravel 8. But they does not provide as default you have to follow few step to do auth.

You have to follow few step to make auth in your laravel 8 project.

First of all you need to install laravel/ui package as like bellow:

Step-1:Download Laravel 8 Fresh New Setup

First, we require to download the laravel fresh setup. Utilize the below command and download fresh incipient laravel setup :

Here I will give you full example for laravel 8 form validation example. So let's follow bellow step by step.

Step 1 : Install Laravel 8

In the first step, we require to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

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

After, that you can run following command and check ui commands info.

php artisan ui --help

Next, You can use following commands for creating auth:

Using Bootstrap:
php artisan ui bootstrap --auth
Using Vue:
php artisan ui vue --auth
Using React:
php artisan ui react --auth

Now, you need to run npm command, otherwise you can not see better layout of login and register page.

Install NPM:
npm install
Run NPM:
npm run dev

If you have the Unhandled repudiation Error: EACCES: sanction gainsaid error, you simply need to utilize sudo afore your command in Ubuntu or macOS systems:

sudo npm install 
sudo npm run dev

Next, we need to add the authentication routes in our Laravel 8 project.

Open the routes/web.php, you should find the following code:

<?php

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

The Auth::routes() method includes the routes for authenticate, registration, logout, and password reset. This method along with the habitation route was integrated when integrated the auth scaffolding in the precedent section.

Protect Routes in your Laravel 8

In a web application, you integrate authentication for primarily bulwarking some pages or routes for unauthorized access.

In Laravel, you can protect a route using a middelware.

Laravel has a builtin auth middleware, which subsists in Illuminate\Auth\Middleware\Authenticate. It's withal registered in the HTTP kernel of your app, you can simply integrate it to your desired route to obviate unauthenticated users from accessing it.

Let's see how the home page is protected so we can protect other pages in the same way:

Open the app/Http/Controllers/HomeController.php file. It has the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
    {
        return view('home');
    }
}

In the constructor (the __construct() method) of the controller, you can optically discern a call to the middleware() method with the auth middleware.

The middleware() method can be either called from the controller or the route definition. So let's remove the call from the controller. Next, open the routes/web.php file and update the home route defintion as follows:

Register Page



We now have a better looking UI. This is a screenshot of the login page:



If you register for an account, you'll be authenticated in and redirected to the /home path which is mapped to a HomeController where you can withal invoke the logout method:

Conclusion

In this tutorial, we have integrated authentication to our CRM app built with Laravel 8.

Integrating authentication is Laravel 8 is a breeze as you have optically discerned since the framework provides a consummate auth system with register, authenticate, logout and password reset out of the box that can be adequate in many cases. But if you optate to handle special requisites you can additionally provide your custom auth controllers but you don't require to implement the base functionalities from scratch, you can utilize the sundry authentication accommodations available from the Auth facade.

If you have utilized Laravel afore Laravel 8, you'll find some transmutations in Laravel 6. For example, the auth routes and views are now part of the dissevered laravel/ui package that you require to install in your project utilizing Composer afore you can call the php artisan ui vue --auth command that superseded the Laravel 5' artisan php artisan make:auth command.

It will work great.

I hope it will help you

Post a Comment

0 Comments