Fakultas Ilmu Komputer UI

Commit 5e72b905 authored by i.gede97@ui.ac.id's avatar i.gede97@ui.ac.id
Browse files

Tambah username di JWT claims

parent b157ba8a
Pipeline #133957 passed with stage
in 1 minute and 50 seconds
from django.urls import path, include
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
TokenVerifyView
)
......@@ -10,7 +9,7 @@ app_name = 'oauth'
urlpatterns = [
path('api-auth/', include('rest_framework.urls')),
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/', views.MyTokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
path('api/create-user/', views.UserCreate.as_view()),
......
......@@ -10,6 +10,9 @@ from djangorestframework_camel_case.parser import (CamelCaseJSONParser,
from djangorestframework_camel_case.render import (CamelCaseJSONRenderer,
CamelCaseBrowsableAPIRenderer)
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView
class UserCreate(generics.CreateAPIView):
serializer_class = UserSerializer
permission_classes = [AllowAny]
......@@ -50,3 +53,17 @@ class Logout(generics.CreateAPIView):
except Exception as err:
error_message = {'error': err}
raise ValidationError(error_message)
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super().get_token(user)
# Add custom claims
token['username'] = user.username
return token
class MyTokenObtainPairView(TokenObtainPairView):
serializer_class = MyTokenObtainPairSerializer
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment