Challenge - 5 Problems
Sanctum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Sanctum token creation code?
Consider this Laravel controller method using Sanctum to create a token for a user. What will be the output JSON response?
Laravel
public function login(Request $request) {
$user = User::where('email', $request->email)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
return response()->json(['message' => 'Invalid credentials'], 401);
}
$token = $user->createToken('api-token')->plainTextToken;
return response()->json(['token' => $token]);
}Attempts:
2 left
💡 Hint
The createToken method returns a token object with a plainTextToken property.
✗ Incorrect
If the user exists and password matches, createToken generates a new token string. The response returns this token in JSON under 'token'.
❓ lifecycle
intermediate1:30remaining
Which middleware is required to protect Sanctum API routes?
You want to protect your API routes so only authenticated users with valid Sanctum tokens can access them. Which middleware should you apply to these routes?
Attempts:
2 left
💡 Hint
Sanctum uses a specific guard for API token authentication.
✗ Incorrect
The 'auth:sanctum' middleware ensures the request has a valid Sanctum token and authenticates the user accordingly.
🔧 Debug
advanced2:30remaining
Why does this Sanctum token authentication fail with 401 Unauthorized?
This API route uses 'auth:sanctum' middleware but always returns 401 Unauthorized even with a valid token sent in the Authorization header. What is the most likely cause?
Laravel
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); }); // Client sends header: Authorization: Bearer <valid_token>
Attempts:
2 left
💡 Hint
Check if the user model supports Sanctum tokens.
✗ Incorrect
If the user model lacks the 'HasApiTokens' trait, Sanctum cannot authenticate tokens for that user, causing 401 errors.
📝 Syntax
advanced1:30remaining
Which code snippet correctly revokes all Sanctum tokens for the authenticated user?
You want to log out a user by deleting all their API tokens. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
The tokens() method returns a relationship to all tokens.
✗ Incorrect
The tokens() method returns a collection of all tokens for the user. Calling delete() on it removes all tokens.
🧠 Conceptual
expert3:00remaining
What is the main difference between Sanctum's SPA authentication and API token authentication?
Sanctum supports two main authentication methods: SPA authentication using cookies and API token authentication using tokens. What is the key difference in how they authenticate users?
Attempts:
2 left
💡 Hint
Think about how browsers handle cookies and headers differently.
✗ Incorrect
Sanctum's SPA authentication uses Laravel's session cookies and CSRF tokens to authenticate requests, while API token authentication uses bearer tokens sent in headers without sessions.