eadwinCode / django-ninja-jwt

A JSON Web Token authentication plugin for the Django REST Framework.
https://eadwincode.github.io/django-ninja-jwt/
MIT License
149 stars 21 forks source link

Unable To Customize Token Claims on TOKEN_VERIFY_INPUT_SCHEMA #71

Closed lanzclintonv closed 6 months ago

lanzclintonv commented 6 months ago

Hello, I have an issue with modifying the token claims when using /verify/. I followed through the docs to modify TOKEN_VERIFY_INPUT_SCHEMA

By default, the verify's response is {} and I wanted to add refresh, access, and user there.


from ninja_jwt.schema import (
    TokenObtainInputSchemaBase,
    TokenVerifyInputSchema,
    InputSchemaMixin,
)

class CustomVerifyInputSchema(TokenVerifyInputSchema, InputSchemaMixin):
    token: str

    @classmethod
    def get_response_schema(cls) -> Type[Schema]:
        return CustomVerifyOutSchema

    def to_response_schema(self):
        try:
            access_token = AccessToken(self.token)
            user_id = access_token["user_id"]

            User = get_user_model()
            user = User.objects.get(id=user_id)
            user_schema = UserSchema.from_orm(user)

            values = {
                "refresh": "your_refresh_token_here", 
                "access": self.token,
                "user": user_schema.dict(),
            }

           return values
        except (KeyError, ValidationError, get_user_model().DoesNotExist) as e:
            raise e

I've also defined it in settings.py of course.

eadwinCode commented 6 months ago

@lanzclintonv Thanks for identifying this error. I am working on a fix for it now and it will be up soon.

eadwinCode commented 6 months ago

@lanzclintonv try the new release v5.3.1 and if that gets the problem solved, kindly close this issue.

lanzclintonv commented 6 months ago

@eadwinCode happy to inform that v5.3.1 solved my issue. Thank you!