0
0
Laravelframework~20 mins

Token management in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Token Mastery in Laravel
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel Sanctum token creation code?
Consider the following Laravel code snippet that creates a token for a user. What will be the output of return $token;?
Laravel
<?php
$user = App\Models\User::find(1);
$token = $user->createToken('api-token')->plainTextToken;
return $token;
AA string representing the plain text token, e.g., '1|randomcharacters...'
BAn object containing token details including expiration
CA boolean true indicating token creation success
DNull, because the token is not returned by default
Attempts:
2 left
💡 Hint
Remember that createToken returns a NewAccessToken object, and plainTextToken is a string.
state_output
intermediate
2:00remaining
What is the value of $user->tokens()->count() after revoking tokens?
Given a user with 3 active tokens, what will be the count after running this code?
$user->tokens()->delete();
Laravel
<?php
$user = App\Models\User::find(1);
// Assume user has 3 tokens
$user->tokens()->delete();
$count = $user->tokens()->count();
return $count;
Anull
B3
C0
DThrows an exception
Attempts:
2 left
💡 Hint
Deleting tokens removes them from the database.
📝 Syntax
advanced
2:00remaining
Which option correctly revokes a specific token by its ID?
You want to revoke a token with ID 5 for a user. Which code snippet does this correctly?
A$user->tokens()->remove(5);
B$user->tokens()->find(5)->revoke();
C$user->tokens()->destroy(5);
D$user->tokens()->where('id', 5)->delete();
Attempts:
2 left
💡 Hint
Tokens are Eloquent models; deleting by query is common.
🔧 Debug
advanced
2:00remaining
Why does this token authentication fail in Laravel Sanctum?
Given this middleware code, why does token authentication fail?
public function handle($request, Closure $next) {
  if (! $request->user()) {
    return response()->json(['error' => 'Unauthenticated'], 401);
  }
  return $next($request);
}
AThe user model does not implement HasApiTokens trait
BThe token is not sent in the Authorization header as Bearer token
CThe middleware does not call <code>auth()->check()</code>
DThe middleware should return <code>$next($request->user())</code> instead
Attempts:
2 left
💡 Hint
Check how Sanctum expects tokens to be sent in requests.
🧠 Conceptual
expert
3:00remaining
Which statement about Laravel Sanctum token abilities is true?
Consider token abilities (scopes) in Laravel Sanctum. Which option correctly describes their behavior?
AAbilities restrict what actions a token can perform; checking abilities is done via <code>$request->user()->tokenCan('ability')</code>
BAbilities automatically expire tokens after 24 hours
CAbilities are stored encrypted and cannot be read by the application
DAbilities are only used for SPA authentication, not API tokens
Attempts:
2 left
💡 Hint
Think about how you limit token permissions in Laravel Sanctum.