Hey there, I'm excited to show you how to create multiple authentication systems in Laravel 10. It's a nifty feature that allows your web application to have different types of users, like admins and customers, each with their own unique access and privileges.
In this guide, I'll walk you through the steps, making it as simple as possible. By the end, you'll have a solid grasp of how to manage distinct user roles and boost the security and flexibility of your Laravel-based projects.
Let's dive into laravel 10 multiple authentication.
If you haven't already, start by creating a new Laravel project using Composer:
composer create-project laravel/laravel your-project-name
cd your-project-name
Configure your database connection in the .env
file. You'll likely want a separate table for each type of user.
Laravel provides a default user authentication system. You can generate it with the following commands.
composer require laravel/ui
php artisan ui bootstrap --auth
npm install
npm run dev
This will create the basic user authentication scaffolding.
Open the migration files created earlier in the database/migrations
directory.
creates_users_table.php
$table->boolean('is_admin')->nullable();
Open the generated model file User.php
<?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',
];
}
Run the migrations to add a new column to the user table:
php artisan migrate
Laravel's Artisan command to generate a new middleware. For example, let's create middleware for admin authentication.
php artisan make:middleware AdminMiddleware
This command will create a new middleware file named AdminMiddleware.php
in the app/Http/Middleware
directory.
<?php
namespace App\Http\Middleware;
use Closure;
class AdminMiddleware
{
/**
* 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.");
}
}
To make the middleware available for your routes, add it to the app/Http/Kernel.php
file in the $routeMiddleware
array. Assign it a unique key, like 'admin.auth,' and specify the fully-qualified class name of the middleware:
protected $routeMiddleware = [
// ...
'admin.auth' => \App\Http\Middleware\AdminMiddleware::class,
];
In the routes/web.php
and routes/api.php
files, define routes for each type of user, and specify the middleware to use. You might have routes like:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('admin/dashboard', [HomeController::class, 'adminDashboard'])->name('admin.dashboard')->middleware('auth:admin');
Add adminDashboard method in app/Http/Controllers/HomeController file.
<?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 adminDashboard()
{
return view('admin_dashboard');
}
}
Now, create a blade file to resources/views/admin_dashboard.blade.php file.
@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">How to Create Multiple Authentication in Laravel 10</div>
<div class="card-body">
@if(auth()->user()->is_admin == 1)
<a href="{{url('admin/routes')}}">This is Admin User</a>
@else
<div class=”panel-heading”>This is Normal User</div>
@endif
</div>
</div>
</div>
</div>
</div>
@endsection
Run laravel application using the following command.
php artisan serve
You might also like:
- Read Also: Laravel 10 React Auth Scaffolding Example
- Read Also: Laravel 10 REST API Authentication using Sanctum
- Read Also: Google Two-Factor Authentication (2FA) in Laravel 10
- Read Also: How To Create CRUD Operation In Laravel 10 Livewire