Implementing Multi-Authentication with Guards in Laravel
This guide provides a detailed explanation of how to implement multi-authentication using guards in a Laravel application. This is useful for applications that need to support multiple user roles (e.g., admins, customers, employees) with different authentication requirements.
Understanding Guards in Laravel
What are Guards?
In Laravel, guards define how users are authenticated for each request. Laravel comes with a session guard which maintains the state of a user using session storage and cookies.
How Guards Work:
Guards work by using providers to retrieve users from the database. You can have multiple guards in an application, each configured to use a different provider and thus manage different types of users.
Setting Up Multi Authentication
Step-by-Step Configuration:
1. Create User Models:
Create different models for each user type. For instance, Admin and Customer.
php artisan make:model Admin -m
php artisan make:model Customer -m
2. Migrate the Databases:
Define schema for
admins
andcustomers
tables in the migration files and run the migrations.
php artisan migrate
3. Modifying the Auth Configuration File:
Open
config/auth.php
and add guards and providers for the new user types.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
],
4. Creating Middleware for Guards
Create middleware to handle authentication for different user types.
php artisan make:middleware AdminMiddleware
php artisan make:middleware CustomerMiddleware
In AdminMiddleware
:
public function handle($request, Closure $next)
{
if (!Auth::guard('admin')->check()) {
return redirect('/login/admin');
}
return $next($request);
}
In CustomerMiddleware
:
phpCopy codepublic function handle($request, Closure $next)
{
if (!Auth::guard('customer')->check()) {
return redirect('/login/customer');
}
return $next($request);
}
Register the middleware in app/Http/Kernel.php
.
protected $routeMiddleware = [
'admin' => \App\Http\Middleware\AdminMiddleware::class,
'customer' => \App\Http\Middleware\CustomerMiddleware::class,
];
5. Implementing Controllers for Different User Types
Create authentication controllers for different user types.
php artisan make:controller AdminController
php artisan make:controller CustomerController
In AdminController
:
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::guard('admin')->attempt($credentials)) {
return redirect()->intended('/admin/dashboard');
}
return back()->withErrors(['email' => 'Invalid credentials.']);
}
In CustomerController
:
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::guard('customer')->attempt($credentials)) {
return redirect()->intended('/customer/dashboard');
}
return back()->withErrors(['email' => 'Invalid credentials.']);
}
6. Setting Up Routes
Define routes for different user types.
Route::prefix('admin')->group(function () {
Route::get('login', [AdminController::class, 'showLoginForm']);
Route::post('login', [AdminController::class, 'login']);
Route::middleware('admin')->group(function () {
Route::get('dashboard', [AdminController::class, 'dashboard']);
});
});
Route::prefix('customer')->group(function () {
Route::get('login', [CustomerController::class, 'showLoginForm']);
Route::post('login', [CustomerController::class, 'login']);
Route::middleware('customer')->group(function () {
Route::get('dashboard', [CustomerController::class, 'dashboard']);
});
});
Testing the Multi Authentication Setup
Ensure you have forms for admin and customer logins.
Test logging in as an admin and accessing admin routes.
Test logging in as a customer and accessing customer routes.
Verify that unauthenticated users are redirected appropriately.
Conclusion
By following this guide, you should be able to set up multi-authentication in a Laravel application using guards. This allows different user roles to have separate authentication processes and access controls, enhancing the security and organization of your application.
Please login or create new account to add your comment.