0
0
Laravelframework~30 mins

Authentication guards in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Authentication Guards in Laravel
📖 Scenario: You are building a simple Laravel web app that has two types of users: admins and customers. You want to protect certain pages so only admins can access them, and other pages so only customers can access them.This is like having two different keys for two different rooms in a house. Each key only opens its own room.
🎯 Goal: Build a Laravel app that uses authentication guards to restrict access to routes based on user type.You will create two guards: one for admins and one for customers, then protect routes accordingly.
📋 What You'll Learn
Create a users array with admin and customer users
Define two guards: admin and customer
Use middleware to protect routes based on guards
Create routes that only admins or customers can access
💡 Why This Matters
🌍 Real World
Many web apps have different user roles needing separate login and access control. Authentication guards help manage this cleanly.
💼 Career
Understanding Laravel authentication guards is essential for backend developers working on secure multi-role applications.
Progress0 / 4 steps
1
Create users array with admin and customer roles
Create a PHP array called users with two entries: one with 'username' => 'adminUser' and 'role' => 'admin', and another with 'username' => 'customerUser' and 'role' => 'customer'.
Laravel
Need a hint?

Use a PHP array with two associative arrays inside, each with keys username and role.

2
Define admin and customer guards in config/auth.php
In the config/auth.php file, add two guards inside the 'guards' array: one named 'admin' using 'session' driver and 'admins' provider, and another named 'customer' using 'session' driver and 'customers' provider.
Laravel
Need a hint?

Inside the 'guards' array, add two arrays with keys driver and provider for each guard.

3
Protect routes using admin and customer middleware
In the routes/web.php file, create two routes: /admin/dashboard protected by 'auth:admin' middleware, and /customer/profile protected by 'auth:customer' middleware. Each route should return a simple string like 'Admin Dashboard' or 'Customer Profile'.
Laravel
Need a hint?

Use Route::get with a closure returning a string, and add ->middleware('auth:admin') or ->middleware('auth:customer').

4
Complete providers for admins and customers
In the config/auth.php file, add two providers inside the 'providers' array: one named 'admins' using 'eloquent' driver and App\Models\Admin::class as model, and another named 'customers' using 'eloquent' driver and App\Models\Customer::class as model.
Laravel
Need a hint?

Inside 'providers', add two arrays with keys driver and model for admins and customers.