我有一些情况,我在django中使用这些软件包进行rest-api和身份验证:
django-rest-framework
(REST API提供商)django-allauth
(用户身份验证)django-rest-framework-jwt
(支持JSON-Web-Token)在django-allauth,我强制要求在注册后确认电子邮件(ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
),使用会话身份验证登录正常并遵循此选项,但jwt生成令牌并忽略此选项。
我该如何处理?
答案 0 :(得分:4)
最后我找到了答案。
我们需要覆盖ObtainJSONWebToken
class:
def post(self, request):
serializer = self.get_serializer(data=request.DATA)
if serializer.is_valid():
user = serializer.object.get('user') or request.user
# check if settings swith is on / then check validity
if settings.ACCOUNT_EMAIL_VERIFICATION == settings.ACCOUNT_EMAIL_VERIFICATION_MANDATORY:
email_address = user.emailaddress_set.get(email=user.email)
if not email_address.verified:
return Response(status=403, data='E-mail is not verified.')
token = serializer.object.get('token')
response_data = jwt_response_payload_handler(token, user, request)
return Response(response_data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
注意:请注意,请使用设置开关ACCOUNT_EMAIL_VERIFICATION
检查电子邮件的有效性,因为我们需要保持此动态。