IT/Django

How to create Djoser custom token strategy more detail info(jwt 정보추가)

bepuri 2023. 6. 1. 17:08
728x90

1. Make new CustomTokenStrategy inherit TokenStrategy

from djoser.social.token.jwt import TokenStrategy


# REf. https://stackoverflow.com/questions/65934755/django-how-do-i-return-jwt-with-custom-claim-after-user-sign-up
class CustomTokenStrategy(TokenStrategy):
    @classmethod
    def obtain(cls, user):
        from rest_framework_simplejwt.tokens import RefreshToken

        refresh = RefreshToken.for_user(user)
        refresh["username"] = user.username
        return {"access": str(refresh.access_token), "refresh": str(refresh)}

2. Update settings.py for CustomTokenStrategy

DJOSER = {
...
    "SOCIAL_AUTH_TOKEN_STRATEGY": "backend.token.CustomTokenStrategy",
...
}

You can add whatever you want.

Djoser uses rest_framework_simplejwt so the package is wrapped.

So sometimes you want to change something about jwt things. you need to change more codes not only rest_framework_simplejwt also Djoser.

Make sure that.

728x90