0
0
Laravelframework~10 mins

API authentication with Sanctum 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 enable Sanctum middleware for API routes.

Laravel
Route::middleware('[1]')->get('/user', function (Request $request) {
    return $request->user();
});
Drag options to blanks, or click blank then click option'
Aauth:sanctum
Bguest
Cweb
Dapi
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'auth' or 'api' middleware instead of 'auth:sanctum'.
Forgetting to add middleware to the route.
2fill in blank
medium

Complete the code to issue a token for the authenticated user.

Laravel
$token = $user->createToken('[1]')->plainTextToken;
Drag options to blanks, or click blank then click option'
Asession
Bpassword
Cauth
Dapi-token
Attempts:
3 left
💡 Hint
Common Mistakes
Using reserved words like 'password' or 'auth' as token names.
Leaving the token name empty.
3fill in blank
hard

Fix the error in the middleware registration for Sanctum in the HTTP kernel.

Laravel
'api' => [
    'throttle:api',
    '[1]',
],
Drag options to blanks, or click blank then click option'
Aauth
Bauth:sanctum
Cguest
Dweb
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'auth' instead of 'auth:sanctum'.
Omitting the Sanctum middleware from the 'api' group.
4fill in blank
hard

Fill both blanks to complete the Sanctum token revocation code.

Laravel
$user->tokens()->where('[1]', '[2]')->delete();
Drag options to blanks, or click blank then click option'
Aid
Bname
Ctoken
Dcreated_at
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' or 'created_at' instead of 'name' to filter tokens.
Trying to delete tokens without filtering.
5fill in blank
hard

Fill all three blanks to complete the middleware and route group setup for Sanctum API authentication.

Laravel
Route::middleware(['[1]', '[2]'])->prefix('[3]')->group(function () {
    Route::get('/profile', function () {
        return auth()->user();
    });
});
Drag options to blanks, or click blank then click option'
Aauth:sanctum
Bthrottle:api
Capi
Dguest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'auth' instead of 'auth:sanctum'.
Missing the 'throttle:api' middleware.
Incorrect route prefix.