Complete the code to check if a user is logged in using Laravel's built-in authentication.
<?php if ([1]) { echo 'User is authenticated'; } else { echo 'User is not authenticated'; }
auth()->guest() which checks if user is NOT logged in.In Laravel, Auth::check() returns true if the user is logged in.
Complete the code to protect a route so only authenticated users can access it.
Route::get('/dashboard', function () { return view('dashboard'); })->[1]('auth');
guard which is for authentication drivers.authCheck or secure.Laravel uses middleware('auth') to restrict routes to authenticated users.
Fix the error in the code to retrieve the authenticated user's email.
<?php
$userEmail = [1]->email;
echo $userEmail;email directly on auth() or Auth facade.Auth::user() returns the authenticated user object; then you access email.
Fill both blanks to create a middleware that redirects guests to login.
<?php
public function handle($request, Closure $next)
{
if ($request->user() [1] null) {
return redirect()->route([2]);
}
return $next($request);
}The middleware checks if user is null (not logged in) and redirects to the login route.
Fill all three blanks to create a dictionary comprehension that maps user names to emails for authenticated users only.
<?php $users = User::all(); $result = []; foreach ($users as $user) { if ($user->[1]()) { $result[$user->[2]] = $user->[3]; } }
isAuthenticated.We check if user is active (authenticated users), then map name to email.