0
0
Laravelframework~10 mins

Why authentication secures applications in Laravel - Test Your Understanding

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

Complete the code to check if a user is logged in using Laravel's built-in authentication.

Laravel
<?php
if ([1]) {
    echo 'User is authenticated';
} else {
    echo 'User is not authenticated';
}
Drag options to blanks, or click blank then click option'
AUser::isLoggedIn()
Bauth()->guest()
CAuth::check()
DAuth::user()
Attempts:
3 left
💡 Hint
Common Mistakes
Using auth()->guest() which checks if user is NOT logged in.
Trying to call a method on User model directly.
2fill in blank
medium

Complete the code to protect a route so only authenticated users can access it.

Laravel
Route::get('/dashboard', function () {
    return view('dashboard');
})->[1]('auth');
Drag options to blanks, or click blank then click option'
Asecure
Bguard
CauthCheck
Dmiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using guard which is for authentication drivers.
Using non-existent methods like authCheck or secure.
3fill in blank
hard

Fix the error in the code to retrieve the authenticated user's email.

Laravel
<?php
$userEmail = [1]->email;
echo $userEmail;
Drag options to blanks, or click blank then click option'
AAuth::user()
BAuth::email()
CUser::email()
Dauth()->email
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access email directly on auth() or Auth facade.
Using static calls on User model incorrectly.
4fill in blank
hard

Fill both blanks to create a middleware that redirects guests to login.

Laravel
<?php
public function handle($request, Closure $next)
{
    if ($request->user() [1] null) {
        return redirect()->route([2]);
    }
    return $next($request);
}
Drag options to blanks, or click blank then click option'
A==
B'login'
C!==
D'home'
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== which checks for not equal, reversing logic.
Redirecting to 'home' instead of 'login'.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps user names to emails for authenticated users only.

Laravel
<?php
$users = User::all();
$result = [];
foreach ($users as $user) {
    if ($user->[1]()) {
        $result[$user->[2]] = $user->[3];
    }
}
Drag options to blanks, or click blank then click option'
AisAuthenticated
Bname
Cemail
DisActive
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent method isAuthenticated.
Mixing up keys and values in the array.