Complete the code to log in a user using Laravel's Auth facade.
if (Auth::[1](['email' => $email, 'password' => $password])) { // User is logged in }
The attempt method tries to log in a user with given credentials.
Complete the code to log out the current user in Laravel.
Auth::[1]();The logout method logs out the current authenticated user.
Fix the error in the middleware to ensure only guests can access the login page.
public function handle($request, Closure $next)
{
if (!Auth::[1]()) {
return redirect('/home');
}
return $next($request);
}The guest method returns true if the user is not logged in, allowing guest-only access.
Fill both blanks to create a route group that requires authentication and logs out users.
Route::middleware('[1]')->group(function () { Route::post('/logout', [LoginController::class, '[2]']); });
The auth middleware restricts routes to logged-in users, and the logout method logs out the user.
Fill all three blanks to create a login controller method that validates input, attempts login, and redirects.
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.']);
}The validate method checks for 'email' and 'password' fields. The attempt method tries to log in with those credentials.