Skip to content

Commit 8323a11

Browse files
committed
1-2d done
1 parent c3411e6 commit 8323a11

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

djsr/authentication/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44

55
class CustomUser(AbstractUser):
6-
fav_color = models.CharField(blank=True, max_length=120)
6+
fav_color = models.CharField(blank=True, max_length=120)

djsr/authentication/serializers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# djsr/authentication/serializers.py
2+
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
3+
4+
5+
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
6+
7+
@classmethod
8+
def get_token(cls, user):
9+
token = super(MyTokenObtainPairSerializer, cls).get_token(user)
10+
11+
# Add custom claims
12+
token['fav_color'] = user.fav_color
13+
return token

djsr/authentication/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.urls import path
22
from rest_framework_simplejwt import views as jwt_views
3+
from .views import ObtainTokenPairWithColorView
34

45
urlpatterns = [
5-
path('token/obtain/', jwt_views.TokenObtainPairView.as_view(), name='token_create'), # override sjwt stock token
6+
path('token/obtain/', ObtainTokenPairWithColorView.as_view(), name='token_create'),
67
path('token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
78
]

djsr/authentication/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
from django.shortcuts import render
22

3-
# Create your views here.
3+
from rest_framework_simplejwt.views import TokenObtainPairView
4+
from .serializers import MyTokenObtainPairSerializer
5+
6+
7+
class ObtainTokenPairWithColorView(TokenObtainPairView):
8+
serializer_class = MyTokenObtainPairSerializer

0 commit comments

Comments
 (0)