How can I use `phone number` and `otp` in "django-rest-framework-simplejwt" instead of `username` and `password` to generate token?
#urls.py
I just want to know how to set custom fields, for e.g instead of 'username' and 'password' can i get 'phone_number' and otp
from django.urls import path
from . import views
from . import api_views
from django.conf.urls.static import static
from django.conf import settings
from rest_framework_simplejwt.views import (TokenObtainPairView,TokenRefreshView,)
urlpatterns = [
# Api Urls
path('api/',api_views.getRoutes,name="api"),
path('api/register',api_views.registerPatient,name="signup"),
path('api/patient/',api_views.getPatients,name="patients"),
path('api/create/',api_views.CreatePatient,name="create"),
path('api/patient/<str:pk>/',api_views.getPatient,name="patient"),
path('api/update/<str:pk>/',api_views.updatePatient,name="update"),
path('api/delete/<str:pk>/',api_views.deletePatient,name="delete"),
path('api/login/',TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/doctor/',api_views.doctorRegister,name="doctorreg"),
path('api/getroutes/', api_views.getRoutes, name='getroutes'),
path('api/sotp/',api_views.send_otp,name="sotp"),
path('api/votp/',api_views.verify_otp,name="votp"),
path('api/signup/',api_views.registerPatient,name="signup"),
]```
You have to create your own TokenSerializer and TokenView classes, that inherit from TokenObtainPairSerializer and TokenObtainPairView and in which you can define token claims. More info here
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super().get_token(user)
# Add custom claims
token['phone_number'] = user.phone_number
# ...
return token
class MyTokenObtainPairView(TokenObtainPairView):
serializer_class = MyTokenObtainPairSerializer
and in the URL
path('api/login/', MyTokenObtainPairView.as_view(), name='token_obtain_pair'),