Hello, laravel web developers! In this article, we'll see how to create multi-auth using Breeze in laravel 11. In laravel 11, we'll create multiple authentications with Breeze. We will start by installing a fresh Laravel 11 application for multi-auth.
Next, we'll install Laravel Breeze to set up the authentication scaffold. After that, we'll add a "type" column to the users' table to define three user types: User, Agent, and Admin. Finally, we'll create a "role" middleware to manage access permissions based on user roles.
Laravel 11 Breeze Multi Authentication
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel laravel-example
Then, install laravel breeze using the following command.
composer require laravel/breeze --dev
Next, we'll create laravel auth using the following command.
php artisan breeze:install
npm install
npm run build
Next, we'll add the type column into the users table using the following command.
php artisan make:migration add_type_to_users_table
Migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->enum('role',['admin','agent','user'])->default('user');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
Then, migrate the table into the database using the following command.
php artisan migrate
app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'role'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
Next, we'll create a middleware using the following command.
php artisan make:middleware Role
app/Http/Middleware/Role.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class Role
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, $role): Response
{
if ($request->user()->role != $role) {
return redirect('dashboard');
}
return $next($request);
}
}
bootstrap/app.php
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'role' => \App\Http\Middleware\Role::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
Now, define the routes into the web.php file.
routes/web.php
<?php
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\AgentController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
Route::middleware(['auth', 'role:admin'])->group(function(){
Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
});
Route::middleware(['auth', 'role:agent'])->group(function(){
Route::get('/agent/dashboard', [AgentController::class, 'dashboard'])->name('agent.dashboard');
});
Then, we'll create controllers using the following command.
php artisan make:controller AdminController
php artisan make:controller AgentController
app/Http/Controllers/AdminController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function dashboard()
{
return view('admin.dashboard');
}
}
app/Http/Controllers/AgentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AgentController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function dashboard()
{
return view('agent.dashboard');
}
}
Then, create a blade file and add the following HTML code to that file.
resources/views/admin/dashboard.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">
You are admin!
</div>
</div>
</div>
</div>
</x-app-layout>
resources/views/agent/dashboard.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">
You are agent!
</div>
</div>
</div>
</div>
</x-app-layout>
Next, we'll change the AuthenticatedSessionController. When the user logs in, we will redirect them according to their access level.
app/Http/Controllers/Auth/AuthenticatedSessionController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class AuthenticatedSessionController extends Controller
{
/**
* Display the login view.
*/
public function create(): View
{
return view('auth.login');
}
/**
* Handle an incoming authentication request.
*/
public function store(LoginRequest $request): RedirectResponse
{
$request->authenticate();
$request->session()->regenerate();
$url = "dashboard";
if ($request->user()->role == "admin") {
$url = "admin/dashboard";
} else if($request->user()->role == "agent"){
$url = "agent/dashboard";
}
return redirect()->intended($url);
}
/**
* Destroy an authenticated session.
*/
public function destroy(Request $request): RedirectResponse
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
}
}
Then, we'll create a user seeder using the following command.
php artisan make:seeder UserSeeder
database/seeders/UserSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::create([
'name' => 'Admin',
'email' => 'admin@laravel.com',
'password' => Hash::make('123456'),
'role' => 'admin'
]);
User::create([
'name' => 'Agent',
'email' => 'agent@laravel.com',
'password' => Hash::make('123456'),
'role' => 'agent'
]);
User::create([
'name' => 'User',
'email' => 'user@laravel.com',
'password' => Hash::make('123456'),
'role' => 'user'
]);
}
}
Now, run the seeder using the following command.
php artisan db:seed --class=UserSeeder
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like: