Bird
0
0

You want to protect an API endpoint so only users with a valid JWT token can access it, and you want tokens to expire after 5 minutes. Which settings and code changes should you apply?

hard📝 Application Q15 of 15
Django - DRF Advanced Features
You want to protect an API endpoint so only users with a valid JWT token can access it, and you want tokens to expire after 5 minutes. Which settings and code changes should you apply?
ASet TOKEN_EXPIRE_TIME = 300 in settings and use TokenAuthentication() in authentication_classes.
BSet SIMPLE_JWT['ACCESS_TOKEN_LIFETIME'] = timedelta(minutes=5) in settings and use JWTAuthentication() in authentication_classes.
CUse JWTAuthentication() in authentication_classes and set JWT_EXPIRATION_DELTA = 5 in settings.
DUse TokenAuthentication() and manually check token age in the view.
Step-by-Step Solution
Solution:
  1. Step 1: Configure JWT token expiry

    In Django REST Framework Simple JWT, set ACCESS_TOKEN_LIFETIME to 5 minutes using timedelta in settings.
  2. Step 2: Use JWTAuthentication in the view

    Set authentication_classes = [JWTAuthentication()] to enforce JWT token authentication on the endpoint.
  3. Final Answer:

    Set SIMPLE_JWT['ACCESS_TOKEN_LIFETIME'] = timedelta(minutes=5) in settings and use JWTAuthentication() in authentication_classes. -> Option B
  4. Quick Check:

    JWT expiry + JWTAuthentication = A [OK]
Quick Trick: Set ACCESS_TOKEN_LIFETIME and use JWTAuthentication() [OK]
Common Mistakes:
MISTAKES
  • Using TokenAuthentication instead of JWTAuthentication
  • Setting wrong expiry setting names
  • Trying to manually check token expiry in views

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes