簡體   English   中英

如何在“django-rest-framework-simplejwt”中使用“電話號碼”和“otp”而不是“用戶名”和“密碼”來生成令牌?

[英]How can I use `phone number` and `otp` in "django-rest-framework-simplejwt" instead of `username` and `password` to generate token?

#urls.py

我只想知道如何設置自定義字段,例如代替“用戶名”和“密碼”我可以得到“電話號碼”和 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"),
]```

您必須創建自己的TokenSerializerTokenView類,它們繼承自TokenObtainPairSerializerTokenObtainPairView並且您可以在其中定義令牌聲明。 更多信息在這里

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

並在網址中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM