如何在Django和DRF中添加2FA

时间:2019-09-12 07:36:15

标签: python django reactjs redux django-rest-framework

我不确定如何将2FA添加到django项目和DRF中? 我最终将这个DRF与react和redux作为前端连接。

这是我的Django方面:

型号:

# Login API
class LoginAPI(generics.GenericAPIView):
    serializer_class = LoginSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]

        })


# Check token and send back what user is associated with that token
class UserAPI(generics.RetrieveAPIView):
    permission_classes = [
        permissions.IsAuthenticated,
    ]

    serializer_class = UserSerializer

    def get_object(self):
        return self.request.user

我的Django API序列化程序

from django.urls import path, include
from knox import views as knox_views
from accounts.api.views import LoginAPI, UserAPI

urlpatterns = [
    path('api/auth', include('knox.urls')),
    path('api/auth/login', LoginAPI.as_view()),
    path('api/auth/user', UserAPI.as_view()),
    path('api/auth/logout', knox_views.LogoutView.as_view(), name='knox_logout'),

我的Django网址:

from rest_framework import generics, permissions
from rest_framework.response import Response
from knox.models import AuthToken

from accounts.api.serializers import LoginSerializer, UserSerializer


# Login API
class LoginAPI(generics.GenericAPIView):
    serializer_class = LoginSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]

        })


# Check token and send back what user is associated with that token
class UserAPI(generics.RetrieveAPIView):
    permission_classes = [
        permissions.IsAuthenticated,
    ]

    serializer_class = UserSerializer

    def get_object(self):
        return self.request.user

我的django视图:

{{1}}

0 个答案:

没有答案