Hi there! In this guide, I’ll show you how to create a multi-authentication system in Laravel 12 for both Admin and User roles. Whether you're building a hosting service, insurance app, credit or loan portal, or even a health or lawyer dashboard, this setup will help you secure your application efficiently.
We'll cover authentication, dashboards, and routing. This beginner-friendly guide is perfect if you're working on projects like a mortgage calculator, donation platform, or any role-based system.
Install laravel 12 using the following command.
composer create-project laravel/laravel multi-auth-laravel12
cd multi-auth-laravel12
In your env file:
DB_DATABASE=multi_auth_db
DB_USERNAME=root
DB_PASSWORD=
Then run:
php artisan migrate
Create model and migration using the following command:
php artisan make:model Admin -m
php artisan make:model User -m
Edit the admins migration:
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
Then run:
php artisan migrate
Now, setup guards for users and admin.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],
Create a controller using the following command.
php artisan make:controller Admin/Auth/LoginController
php artisan make:controller User/Auth/LoginController
In Admin LoginController, add:
use Illuminate\Support\Facades\Auth;
public function showLoginForm() {
return view('admin.auth.login');
}
public function login(Request $request) {
if (Auth::guard('admin')->attempt($request->only('email', 'password'))) {
return redirect()->route('admin.dashboard');
}
return back()->withErrors(['email' => 'Invalid Credentials']);
}
Repeat similarly for the User LoginController.
In routes/web.php:
// User Routes
Route::get('login', [UserLoginController::class, 'showLoginForm'])->name('login');
Route::post('login', [UserLoginController::class, 'login']);
Route::get('/user/dashboard', function () {
return 'User Dashboard';
})->middleware('auth');
// Admin Routes
Route::prefix('admin')->group(function () {
Route::get('login', [AdminLoginController::class, 'showLoginForm'])->name('admin.login');
Route::post('login', [AdminLoginController::class, 'login']);
Route::get('/dashboard', function () {
return 'Admin Dashboard';
})->middleware('auth:admin');
});
Create login pages in resources/views/admin/auth/login.blade.php and resources/views/user/auth/login.blade.php:
<form method="POST" action="{{ route('admin.login') }}">
@csrf
<input type="email" name="email" required />
<input type="password" name="password" required />
<button type="submit">Login</button>
</form>
Each will redirect to its respective dashboard based on guard.
Creating Laravel 12 Authentication with multi-auth for Admin and User is a crucial feature for any modern application—be it for insurance, credit, health, or donation systems. With clear routing, guards, and role separation, your app stays secure and scalable.
Frequently Asked Questions (FAQs)
Yes, but for more control and customization, manual setup like guards and route groups is often preferred.
Use Auth::guard('admin')->logout();
and Auth::logout();
for respective user roles.
Absolutely! Just create new models, configure guards, and define route groups for each role.
Yes, you can use guards with Laravel Sanctum or Passport to apply multi-auth logic to APIs.
Use custom middleware or override the authenticated()
method in your LoginController
.
You might also like: