如何在"django-rest-framework-simplejwt"中使用"电话号码"和"OTP"而不是"用户名"和"密码"来生成令牌?



#urls.py

我只想知道如何设置自定义字段,例如,我可以获得"phone_number"和otp而不是"username"one_answers"password">

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"),
]```

您必须创建自己的TokenSerializerTokenView类,这些类继承自TokenObtainPairSerializer[/strong>和token ObtainPairview,并且您可以在其中定义令牌声明。更多信息点击这里

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

并且在URL 中

path('api/login/', MyTokenObtainPairView.as_view(), name='token_obtain_pair'),

相关内容

  • 没有找到相关文章

最新更新