An authentication guard in Laravel is a way to define how users are authenticated for each request. It controls how users are retrieved and how their credentials are checked.
Click to reveal answer
beginner
Name two common types of guards in Laravel.
The two common types of guards in Laravel are 'session' guard, which uses sessions and cookies, and 'token' guard, which uses API tokens for stateless authentication.
Click to reveal answer
intermediate
How do you specify which guard to use in Laravel?
You specify the guard by passing its name to the auth() helper or using the 'guard' method, like auth()->guard('web') or auth('api').
Click to reveal answer
beginner
Where are guards configured in a Laravel project?
Guards are configured in the 'config/auth.php' file under the 'guards' array. Each guard defines its driver and provider.
Click to reveal answer
intermediate
What is the role of a user provider in Laravel authentication guards?
A user provider defines how users are retrieved from storage, like a database. Guards use providers to fetch user data for authentication.
Click to reveal answer
Which file in Laravel contains the configuration for authentication guards?
Aconfig/auth.php
Broutes/web.php
Capp/Http/Kernel.php
Dconfig/database.php
✗ Incorrect
The 'config/auth.php' file holds the configuration for authentication guards in Laravel.
What does the 'session' guard in Laravel use to keep users logged in?
AJWT tokens
BCookies and sessions
COAuth tokens
DAPI tokens
✗ Incorrect
The 'session' guard uses cookies and sessions to maintain user login state.
How do you access the 'api' guard in Laravel code?
AAuth::user()
BAuth::guard('web')
Cauth()->user()
Dauth('api')
✗ Incorrect
You access a specific guard by passing its name to the auth() helper, like auth('api').
What is the main purpose of a user provider in Laravel authentication?
ATo retrieve user data from storage
BTo encrypt passwords
CTo manage sessions
DTo handle middleware
✗ Incorrect
User providers define how user data is fetched from storage, such as a database.
Which guard type is best for stateless API authentication?
ACookie guard
BSession guard
CToken guard
DFile guard
✗ Incorrect
Token guards are used for stateless API authentication, often using API tokens.
Explain what an authentication guard is in Laravel and how it works with user providers.
Think about how Laravel checks who a user is and where it gets user info.
You got /3 concepts.
Describe how you would configure and use multiple guards in a Laravel application.
Consider a site with both web users and API users needing different login methods.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of an authentication guard in Laravel?
easy
A. To manage session timeouts
B. To store user passwords securely
C. To define database connections
D. To control how users are authenticated for each request
Solution
Step 1: Understand the role of authentication guards
Guards determine how users are authenticated on each request, like checking credentials or tokens.
Step 2: Differentiate guards from other features
Guards do not store passwords, manage sessions, or define databases; those are handled elsewhere.
Final Answer:
To control how users are authenticated for each request -> Option D
Quick Check:
Authentication guard = controls user authentication [OK]
Hint: Guards decide how users log in and access app [OK]
Common Mistakes:
Confusing guards with password storage
Thinking guards manage database connections
Assuming guards handle session timeouts
2. Which of the following is the correct way to specify a guard in Laravel's auth.php config file?
What will be the output if no admin user is logged in but a regular user is?
medium
A. No admin logged in
B. Error: guard not found
C. Admin logged in
D. No output
Solution
Step 1: Understand what Auth::guard('admin')->check() does
This checks if a user is logged in using the 'admin' guard specifically.
Step 2: Analyze the scenario
No admin user is logged in, so check() returns false, even if a regular user is logged in under a different guard.
Final Answer:
No admin logged in -> Option A
Quick Check:
Guard check false means no user logged in for that guard [OK]
Hint: Guard check only sees users of that guard [OK]
Common Mistakes:
Assuming any logged-in user passes the guard check
Confusing guard names
Expecting error if guard exists
4. Identify the error in this Laravel guard usage:
Auth::guard('web')->login($user);
if (Auth::check()) {
return 'User logged in';
}
medium
A. No error, code works correctly
B. Auth::check() does not check the 'web' guard by default
C. Missing middleware to protect the route
D. Auth::guard('web')->login() requires a user ID, not a user object
Solution
Step 1: Understand Auth::guard('web')->login()
This logs in the user using the 'web' guard explicitly.
Step 2: Check what Auth::check() does
Auth::check() uses the default guard, which may not be 'web', so it might not detect the login.
Final Answer:
Auth::check() does not check the 'web' guard by default -> Option B
Quick Check:
Auth::check() uses default guard, not necessarily 'web' [OK]
Hint: Auth::check() uses default guard, not specified guard [OK]
Common Mistakes:
Assuming Auth::check() checks all guards
Thinking login() needs user ID only
Ignoring middleware importance
5. You want to protect an admin route so only users authenticated with the 'admin' guard can access it. Which is the correct way to apply this in Laravel's route definition?
hard
A. Route::middleware('auth:admin')->group(function () { Route::get('/admin', fn() => 'Admin area'); });
B. Route::middleware('auth:web')->group(function () { Route::get('/admin', fn() => 'Admin area'); });
C. Route::middleware('guest:admin')->group(function () { Route::get('/admin', fn() => 'Admin area'); });
D. Route::middleware('auth')->group(function () { Route::get('/admin', fn() => 'Admin area'); });
Solution
Step 1: Understand middleware usage for guards
The 'auth' middleware accepts a guard name to specify which guard to check for authentication.
Step 2: Choose the middleware that checks 'admin' guard
'auth:admin' ensures only users authenticated with 'admin' guard access the route.
Step 3: Eliminate incorrect options
'auth:web' checks the wrong guard, 'guest:admin' checks for unauthenticated users, and 'auth' without guard uses default guard.