【发布时间】:2019-02-02 02:06:57
【问题描述】:
我正在使用 django 2.1 和 python 3.6....我想使用自定义身份验证对用户进行身份验证,并在验证用户后返回令牌以及电子邮件和 id。
settinge.py
AUTHENTICATION_BACKENDS = (
"users.views.authentication_backend.UserAuthenticationBackend",
)
views.py
class UserAuthenticationBackend(object):
def authenticate(self, request, email=None, password=None):
validate_email(email)
valid_email = True
kwargs = {'email': email}
try:
user = get_user_model().objects.get(**kwargs)
except get_user_model().DoesNotExist:
raise exceptions.AuthenticationFailed('User does not exist.')
if valid_email and user.check_password(password) and user.is_valid is True:
return user
elif valid_email and user.check_password(password) and user.is_valid is False:
raise exceptions.AuthenticationFailed('Sorry your account is suspended temporarily,'
)
else:
raise exceptions.AuthenticationFailed('Sorry, your email and password does not match.')
我想返回如下响应:
{'token':"", 'user': {'id': 1, 'email': 'admin@abc.com', 'is_active': True}}
提前谢谢...
【问题讨论】:
-
身份验证类不适用于返回响应
标签: django python-3.x authentication django-rest-framework jwt