0
0
Laravelframework~10 mins

Login and logout in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to log in a user using Laravel's Auth facade.

Laravel
if (Auth::[1](['email' => $email, 'password' => $password])) {
    // User is logged in
}
Drag options to blanks, or click blank then click option'
Alogout
Bcheck
Clogin
Dattempt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'login' instead of 'attempt' will cause errors.
Using 'check' only checks if user is logged in.
2fill in blank
medium

Complete the code to log out the current user in Laravel.

Laravel
Auth::[1]();
Drag options to blanks, or click blank then click option'
Aattempt
Blogout
Ccheck
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'attempt' tries to log in, not log out.
Using 'check' only checks login status.
3fill in blank
hard

Fix the error in the middleware to ensure only guests can access the login page.

Laravel
public function handle($request, Closure $next)
{
    if (!Auth::[1]()) {
        return redirect('/home');
    }
    return $next($request);
}
Drag options to blanks, or click blank then click option'
Aguest
Blogout
Ccheck
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'check' returns true if user is logged in, opposite of needed.
Using 'logout' here is incorrect.
4fill in blank
hard

Fill both blanks to create a route group that requires authentication and logs out users.

Laravel
Route::middleware('[1]')->group(function () {
    Route::post('/logout', [LoginController::class, '[2]']);
});
Drag options to blanks, or click blank then click option'
Aauth
Blogin
Clogout
Dguest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'guest' middleware allows only guests, not logged-in users.
Using 'login' method does not log out users.
5fill in blank
hard

Fill all three blanks to create a login controller method that validates input, attempts login, and redirects.

Laravel
public function login(Request $request)
{
    $credentials = $request->validate([[1] => ['required', 'email'], [2] => ['required']]);

    if (Auth::[3]($credentials)) {
        return redirect()->intended('/dashboard');
    }

    return back()->withErrors(['email' => 'Invalid credentials.']);
}
Drag options to blanks, or click blank then click option'
A'email'
B'password'
Cattempt
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'check' instead of 'attempt' will not log in the user.
Swapping 'email' and 'password' keys causes validation errors.