implement multiple authentication in Laravel, where different user types (like admin, seller, or customer) can log in with separate guards and dashboards. I’ll provide clear examples along the way.

Step 1: Install Laravel

If you don’t have a Laravel project yet:

composer create-project laravel/laravel multi-auth
cd multi-auth
php artisan serve

Step 2: Create Models and Migrations

Suppose we want Admin and Seller users separate from the default User model.

  1. Create Admin model with migration:

php artisan make:model Admin -m

In the migration file database/migrations/xxxx_create_admins_table.php:

public function up(): void
{
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
  1. Create Seller model with migration:

php artisan make:model Seller -m

In xxxx_create_sellers_table.php:

public function up(): void
{
Schema::create('sellers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
  1. Run migrations:

php artisan migrate

Step 3: Configure Guards and Providers

Open config/auth.php. Add new guards and providers:

'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'seller' => [ 'driver' => 'session', 'provider' => 'sellers', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], 'sellers' => [ 'driver' => 'eloquent', 'model' => App\Models\Seller::class, ], ],

Step 4: Create Controllers for Login

We will make separate login controllers.

php artisan make:controller Admin/Auth/LoginController
php artisan make:controller Seller/Auth/LoginController

Example for Admin login:

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
public function showLoginForm()
{
return view('admin.auth.login');
}

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.']);
}

public function logout()
{
Auth::guard('admin')->logout();
return redirect('/admin/login');
}
}

Seller login controller is similar, just replace admin with seller.

Step 5: Create Routes

In routes/web.php:

// Admin Routes
Route::prefix('admin')->group(function () {
Route::get('login', [App\Http\Controllers\Admin\Auth\LoginController::class, 'showLoginForm']);
Route::post('login', [App\Http\Controllers\Admin\Auth\LoginController::class, 'login']);
Route::post('logout', [App\Http\Controllers\Admin\Auth\LoginController::class, 'logout']);
Route::get('dashboard', function () {
return view('admin.dashboard');
})->middleware('auth:admin');
});// Seller Routes
Route::prefix('seller')->group(function () {
Route::get('login', [App\Http\Controllers\Seller\Auth\LoginController::class, 'showLoginForm']);
Route::post('login', [App\Http\Controllers\Seller\Auth\LoginController::class, 'login']);
Route::post('logout', [App\Http\Controllers\Seller\Auth\LoginController::class, 'logout']);
Route::get('dashboard', function () {
return view('seller.dashboard');
})->middleware('auth:seller');
});

Step 6: Create Login Views

Create Blade files:

  • resources/views/admin/auth/login.blade.php
  • resources/views/seller/auth/login.blade.php

Example:

<form method="POST" action="{{ url('/admin/login') }}">
@csrf
<input type="email" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<button type="submit">Login</button>
</form>

Seller view is the same but change the action to /seller/login.

Step 7: Protect Routes Using Middleware

Laravel allows you to protect routes by guard:

Route::get('/admin/dashboard', function(){})->middleware('auth:admin');
Route::get('/seller/dashboard', function(){})->middleware('auth:seller');

Categorized in:

Laravel,