In this tutorial you will learn about the Laravel 8 Multi Authentication Example Tutorial and its application with practical example.
In this Laravel 8 Multi Authentication Example Tutorial I’ll show you how to create multi auth system using middleware in laravel 8. In Laravel Multiple authentication system multiple users can log in in one application according to their roles.
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, In this example 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.
Laravel 8 Multi Authentication Example Tutorial
In this step by step Laravel 8 Multi Authentication Example we will learn to implement multi auth system using middleware in laravel 8.
- Step 1: Install Laravel 8 App
- Step 2: Connecting App to Database
- Step 3: Setting up migration and model
- Step 4: Create Middleware and Setting up
- Step 5: Define Route
- Step 6: Create Methods in Controller
- Step 7: Create Blade View
- Step 8: Start Development Server
Step 1: Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel LaraMulti |
Step 2: Connecting App to Database
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=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3. Setting up migration and model
In this step we will add is_admin column in the users table using laravel mirgration. So, Open the creates_users_table.php migration file, which is placed on Database/migration and update the following field for admin.
1 |
$table->boolean('is_admin')->nullable(); |
Next open app/User.php and update the below field name is_admin here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'is_admin' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 4: Install Laravel UI
Then install laravel 8 UI in your project using the below command:
1 |
composer require laravel/ui |
Now, execute the below command on terminal for creating login, registration, forget password and reset password blade files:
1 |
php artisan ui bootstrap --auth |
Then execute the following commands:
1 2 |
npm install npm run dev |
Step 5: Create Middleware and Setting up
In this step we will create middleware for checking the user’s role. It is an admin or normal user.
1 |
php artisan make:middleware IsAdmin |
Once the above command is executed a middleware is created. Noe go to app/Http/middleware and Implement the logic to check user role.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace App\Http\Middleware; use Closure; class IsAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if(auth()->user()->is_admin == 1){ return $next($request); } return redirect(‘home’)->with(‘error’,"You don't have admin access."); } } |
Now, we will register this middleware in the app/Http/Kernel.php. Now, open kernal.php and add the following $routeMiddleware property in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
.... 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, 'is_admin' => \App\Http\Middleware\IsAdmin::class, ]; .... |
Step 5: Define Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\HomeController; Route::get('admin/home', [HomeController::class, 'adminHome'])->name('admin.home')->middleware('is_admin'); |
Step 6: Create Methods in Controller
Now open the HomeController.php file, which is placed on app/Http/Controllers/ directory. Then add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function adminHome() { return view('adminHome'); } } |
Step 7: Create Blade View
Now, create two blade view files first is display home page and second is display after login. Open the resources/views/home.blade. file and update the below code.
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 |
Now, I checked the user profile. If it is admin, it will navigate to the admin area. Otherwise, it will redirect to users area. Create admin.blade.php file inside resources/views/ directory and update the following code:
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 |
Step 8: Run development server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |