JWT token error "Token has no id" Django Rest Framework

  • I'm trying to access data(user_id) from the custom simple jwt token.
  • But I'm getting this error. "message": "Token has no id"
    "detail": "Given token not valid for any token type",
    "code": "token_not_valid",
    "messages": [
            "token_class": "AccessToken",
            "token_type": "access",
            "message": "Token has no id"
  • this is my jwt_custom.py
def generate_access_token(user):

    access_token_payload = {
        'user_id': user.id,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, minutes=20),
        'iat': datetime.datetime.utcnow(),
    access_token = jwt.encode(access_token_payload, settings.SECRET_KEY, algorithm='HS256')
    return access_token

def generate_refresh_token(user):
    refresh_token_payload = {
        'user_id': user.id,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(days=7),
        'iat': datetime.datetime.utcnow()
    refresh_token = jwt.encode(
        refresh_token_payload, settings.SECRET_KEY, algorithm='HS256')

    return refresh_token
  • this is my views.py here I'm creating a token when the user login.
class LoginView(APIView):
    serializer_class = LoginSerializer

    def post(self, request, **kwargs):
        serializer = self.serializer_class(data=request.data)
           generate_access_token = jwt_custom.generate_access_token(user)
           generate_refresh_token = jwt_custom.generate_refresh_token(user)
           return Response({"response": True, "return_code": "login_success",'refresh': str(generate_access_token), 'access': str(generate_refresh_token), "result": {"user": user_serializer.data}, "message": success["login_success"]},  status=200)
  • this is views.py here I'm trying to get data through the token.
class UserPosts(APIView):
    def post(self, request, user_id=None, *args, **kwargs):

        get_token = request.META.get('Authorization', None)
            payload = jwt.decode(get_token, settings.SECRET_KEY, algorithms=['HS256']).decode('utf-8')
        except jwt.ExpiredSignatureError:
            return Response({'expired refresh token, please login again.': "notFound"})

        get_user_data = request.data["user_id"]
            post = Post.objects.filter(user_id=payload.get('user_id')).order_by('-created_on')
            paginator = CustomPageNumberPagination()
            page = paginator.paginate_queryset(post, request)
  • I'm not able to find the issue.

Try to set the following in your settings.

'USER_ID_FIELD': 'user_id'
Back to Top