Skip to content

Unable To Customize Token Claims on TOKEN_VERIFY_INPUT_SCHEMA #71

@lanzclintonv

Description

@lanzclintonv

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions