0
0
Laravelframework~20 mins

Authentication guards in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Authentication Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a user tries to access a route protected by the 'auth' guard without being logged in?

Consider a Laravel route protected by the default 'auth' guard. What is the behavior when an unauthenticated user tries to access this route?

Laravel
Route::middleware('auth')->get('/dashboard', function () {
    return 'Dashboard';
});
AThe user sees a 404 Not Found error.
BThe user is redirected to the login page automatically.
CThe user can access the route without restrictions.
DThe application throws a server error (500).
Attempts:
2 left
💡 Hint

Think about what Laravel does when authentication fails on routes protected by 'auth' middleware.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly defines a custom guard in Laravel's auth configuration?

Given the need to add a custom guard named 'admin' using the 'session' driver and 'admins' provider, which option is correct?

A"admin" => ["driver" => "token", "provider" => "admins"]
B"admin" => ["driver" => "session"]
C"admin" => ["provider" => "admins", "driver" => "session"]
D"admin" => ["driver" => "session", "provider" => "admins"]
Attempts:
2 left
💡 Hint

Remember the order of keys does not matter, but all required keys must be present.

🔧 Debug
advanced
2:30remaining
Why does the following code throw an error when trying to authenticate with a custom guard?

Examine this Laravel controller snippet:

if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('/admin/dashboard');
}

The error says: Call to undefined method attempt() on null. What is the cause?

AThe 'attempt' method does not exist on any guard in Laravel.
BThe 'email' and 'password' keys are incorrect; they should be 'username' and 'pass'.
CThe 'admin' guard is not defined in config/auth.php, so Auth::guard('admin') returns null.
DThe Auth facade is not imported, causing the method call to fail.
Attempts:
2 left
💡 Hint

Check if the guard name matches the configuration.

🧠 Conceptual
advanced
2:30remaining
What is the main difference between 'session' and 'token' drivers in Laravel authentication guards?

Laravel supports multiple drivers for authentication guards. What is the key difference between the 'session' and 'token' drivers?

A'session' driver uses cookies to maintain login state, while 'token' driver uses API tokens for stateless authentication.
B'session' driver is for API authentication, and 'token' driver is for web sessions.
C'session' driver requires manual token management, 'token' driver automatically manages sessions.
D'session' driver encrypts passwords, 'token' driver does not.
Attempts:
2 left
💡 Hint

Think about how web apps keep users logged in versus how APIs authenticate requests.

state_output
expert
3:00remaining
What is the value of Auth::guard('web')->user() after logging in with a custom user provider?

Assume you have a custom user provider configured for the 'web' guard that fetches users from a special table. After a successful login, what does Auth::guard('web')->user() return?

AAn instance of the user model returned by the custom provider representing the logged-in user.
BNull, because custom providers do not support the user() method.
CA boolean true indicating successful authentication.
DAn array of all users in the custom table.
Attempts:
2 left
💡 Hint

Consider what the user() method returns after authentication.