In this tutorial you will learn about the Laravel 7/6 Multi Auth( Authentication) Example Tutorial and its application with practical example.
In this Laravel multi (auth) authentication tutorial I’ll show you how to create multi auth system in laravel 7/6. In Laravel Multiple authentication multiple users can log in in one application according to roles.
Laravel 7/6 Multi Auth( Authentication) Example Tutorial
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 this laravel multi auth system, we will create a middleware for checking the user’s role. It is an admin or normal user. We will create middleware name admin and configuration in the kernal.php file and also in the route file.
Install Laravel 6
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= |
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 :-