Last active
March 29, 2023 04:22
-
-
Save lordsarcastic/4fd8f3a5b58d2fd71b79c3189f3720ae to your computer and use it in GitHub Desktop.
Add more details to the access and refresh token gotten from login on SimpleJWT with Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer | |
from .models import User | |
class LoginSerializer(TokenObtainPairSerializer): | |
user_info = serializers.SerializerMethodField() | |
@classmethod | |
def get_token(cls, user: User): | |
token = super().get_token(user) | |
# Add custom claims | |
token["user_info"] = { | |
"uuid": str(user.uuid), | |
"email": user.email, | |
"is_verified": user.is_verified, | |
"first_name": user.first_name, | |
"last_name": user.last_name, | |
"is_staff": user.is_staff, | |
"is_superuser": user.is_superuser, | |
"is_active": user.is_active, | |
} | |
return token |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from . import views | |
app_name = "users" | |
urlpatterns = [ | |
path("login/", views.LoginAPIView.as_view(), name="login"), | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework_simplejwt.views import TokenObtainPairView | |
from .serializers import LoginSerializer | |
class LoginAPIView(TokenObtainPairView): | |
serializer_class = LoginSerializer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment