In this tutorial you will learn about the Laravel 5.8 Multiple Authentication Using Middleware and its application with practical example.
Laravel 5.8 Multiple Authentication Using Middleware
In this Laravel multi (auth) authentication tutorial we will learn how to create separate admin panel or login using middleware. Laravel multi (auth) authentication system allows to create multiple users login in single application.
While developing a laravel application there comes situations where we want create separate user login or separate user/admin panel for users having different privileges or access rights in same application. In laravel this can be achieved in following ways –
1. Laravel multi (auth) authentication using middleware
2. Laravel multi (auth) authentication Using Guards
In one of my previous article we have created separate admin login using laravel multi (auth) authentication using Guards. In this article we will creating separate admin login using laravel multi (auth) authentication system with middleware.
Install Laravel 5.8
First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel larablog |
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=larablog DB_USERNAME=root DB_PASSWORD= |
Generate Application Key
Open terminal and switch to the project directory and run the following command to generate application key and configure cache.
1 |
php artisan key:generate |
1 |
php artisan config:cache |
Set Default String Length
Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file
1 |
use Illuminate\Support\Facades\Schema; |
and inside the boot method set a default string length as given below –
1 |
Schema::defaultStringLength(191); |
So this is how “app/Providers/AppServiceProvider” file looks like –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { public function boot() { // Schema::defaultStringLength(191); } public function register() { // } } |
Migrations and User Model
After installing laravel, you will see the some migration files created inside database/migrations
directory for creating default create users table and password reset table. Open app/database/create_users_table.php migration file and add/update the following field for admin.
1 |
$table->boolean('is_admin')->nullable(); |
After you added a column “is_admin” to user table run the migration using following command.
1 |
php artisan migrate |
After this, go to app/User.php and add is_admin to fillable attribute in the User Model.
1 2 3 |
protected $fillable = [ 'name', 'email', 'password', 'is_admin' ]; |
Authentication Scaffolding
Laravel comes with in-built basic authentication system, use the below command to generate default authentication scaffolding –
1 |
php artisan make:auth |
This command will generate required Controller files, views and add routes in our web.php routes file that are required for basic authentication system.
Create Admin Middleware
Now, we will create a middleware that authenticate users. This middleware will help to make sure who can access the admin area or who can access the normal user area. Use the following command to generate a middleware for Admin.
1 |
php artisan make:middleware Admin |
Next, open app/Http/ Middleware/Admin.php file, and here in handle() method implement the logic to authenticate users like below.
1 2 3 4 5 6 7 |
public function handle($request, Closure $next){ if(auth()->user()->is_admin == 1){ return $next($request); } return redirect('home')->with('error','You have no admin access'); } |
Register Admin Routes
Now, we need to register admin route in $routeMiddleware property in the app/Http/Kernel.php file. Lets open app/Http/Kernel.php file and update the $routeMiddleware property like below –
1 2 3 4 5 6 7 8 9 10 11 12 |
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'admin' => \App\Http\Middleware\Admin::class, ]; |
Set Admin Protected Route
Next, create the route for admin. Open the routes/web.php file and add the following code in it.
routes/web.php
1 |
Route::get('admin/routes', 'HomeController@admin')->middleware('admin'); |
Create Controller Methods
Open the app/Http/Controllers/HomeController.php controller file and create two separate methods to handle admin and normal users.
app/Http/Controllers/HomeController.php
1 2 3 4 5 6 7 8 9 |
public function index(){ return view('home'); } public function admin(){ return view('admin'); } |
Create Blade/View
Now we will have two separate view files, first is for home page and second is for after login admin page. Open the resources/views/home.blade.php file and update the following code.
resources/views/home.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Dashboard</div> <div class="card-body"> @if(auth()->user()->is_admin == 1) <a href="{{url('admin/routes')}}">Admin</a> @else <div class="panel-heading">Normal User</div> @endif </div> </div> </div> </div> </div> @endsection |
Here, if(auth()->user()->is_admin == 1) is used to check user is a normal user or an admin user, it will navigate to the admin area. Otherwise, it will redirect to users area.
Now, create a view file called admin.blade.php in the root of the views folder, and add the following code to it.
resources/views/admin.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@extends('layouts.app') @section('content') <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading btn-primary">WELCOME TO ADMIN ROUTE</div> </div> </div> </div> @endsection |
Start Development Server
Start the development server using following artisan command –
1 |
php artisan serve |
In order to test laravel multi auth system first register a user through laravel register and then change the is_admin=1; and try to login again.
User Home 1 :-
After Login Admin Page :-
User Home 2 :-