0
0
Laravelframework~10 mins

Token management 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 generate a new API token for the authenticated user.

Laravel
$token = auth()->user()->[1]('api-token');
Drag options to blanks, or click blank then click option'
AcreateToken
BgenerateToken
CmakeToken
DnewToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like generateToken.
Trying to call token creation on the auth() helper instead of the user.
2fill in blank
medium

Complete the code to revoke the current user's token.

Laravel
auth()->user()->currentAccessToken()->[1]();
Drag options to blanks, or click blank then click option'
Arevoke
Bdelete
Cremove
Dinvalidate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'revoke' which is not a method on the token model.
Trying to set the token to null instead of deleting.
3fill in blank
hard

Fix the error in the code to check if the token has a specific ability.

Laravel
if ($token->[1]('update-posts')) {
    // allow update
}
Drag options to blanks, or click blank then click option'
Acan
BhasAbility
CcheckAbility
Dallows
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like hasAbility or checkAbility.
Confusing token abilities with user permissions.
4fill in blank
hard

Fill both blanks to create a token with specific abilities and retrieve its plain text token.

Laravel
$token = auth()->user()->[1]('api-token', ['create', 'update']);
$plainTextToken = $token->[2];
Drag options to blanks, or click blank then click option'
AcreateToken
BplainTextToken
Ctoken
DgetToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method instead of a property for the token string.
Confusing token abilities with user roles.
5fill in blank
hard

Fill all three blanks to revoke all tokens except the current one for the authenticated user.

Laravel
auth()->user()->tokens()->where('id', '!=', auth()->user()->[1]()->id)->[2]();
$currentToken = auth()->user()->[3]();
Drag options to blanks, or click blank then click option'
AcurrentAccessToken()->id
Bdelete
CcurrentAccessToken
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'token' instead of 'currentAccessToken' to get the current token.
Trying to revoke tokens without filtering out the current one.