0

我在 Django 中使用了 simpleJWT。登录工作正常,但是我想为刷新令牌实现一个视图。因此,我在请求正文中获取访问和刷新令牌,检查访问令牌是否仍然有效,如果不是,则生成一个新令牌。

但是,此实现似乎不起作用。(使用邮递员使用它请求用户详细信息的给定令牌对其进行测试)

序列化程序.py

class RefreshSerializer(serializers.Serializer):
    accessToken = serializers.CharField(max_length=255, required=True)
    refreshToken = serializers.CharField(max_length=255, required=True)

视图.py

class TokenRefreshView(APIView):
    permission_classes = ()
    authentication_classes = ()

    def post(self, request):
        received_json_data = request.data
        serializer = RefreshSerializer(data=received_json_data)
        if serializer.is_valid():
            accessToken = received_json_data['accessToken']
            refreshToken = received_json_data['refreshToken']
            if (rest_framework_simplejwt.views.token_verify(accessToken).status_code != 200):
                user = rest_framework_simplejwt.authentication.JWTAuthentication.get_user(accessToken)
                refresh = RefreshToken.for_user(user)
                newaccesstoken = str(refresh.access_token)
                return JsonResponse({
                    'accessToken': newaccesstoken
                }, status=200)
            else:
                return JsonResponse({
                    'message': 'Token still valid',
                }, status=403)
        else:
            return JsonResponse({'message': serializer.errors}, status=400)
4

0 回答 0