Bird
0
0

Given this Django REST Framework view snippet using JWTAuthentication, what will happen if the token is expired?

medium📝 component behavior Q13 of 15
Django - DRF Advanced Features
Given this Django REST Framework view snippet using JWTAuthentication, what will happen if the token is expired?
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.views import APIView
from rest_framework.response import Response

class MyView(APIView):
    authentication_classes = [JWTAuthentication()]

    def get(self, request):
        return Response({"user": str(request.user)})
AThe request will succeed but request.user will be None.
BThe request will succeed and return the user even if token expired.
CThe request will be denied with a 401 Unauthorized error.
DThe server will crash with an exception.
Step-by-Step Solution
Solution:
  1. Step 1: Understand JWTAuthentication behavior on expired tokens

    JWTAuthentication checks token expiry and rejects requests with expired tokens by raising an authentication error.
  2. Step 2: Effect on the APIView request

    When token is expired, the request is denied with a 401 Unauthorized response automatically by DRF.
  3. Final Answer:

    The request will be denied with a 401 Unauthorized error. -> Option C
  4. Quick Check:

    Expired JWT causes 401 error = B [OK]
Quick Trick: Expired JWT tokens cause 401 Unauthorized error [OK]
Common Mistakes:
MISTAKES
  • Assuming expired token returns user as None
  • Thinking expired token lets request pass
  • Expecting server crash on expired token

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes