如何在django-rest-auth的http://127.0.0.1:8000/rest-auth/login/中执行我的逻辑? [英] How can I do my logic in `http://127.0.0.1:8000/rest-auth/login/` of `django-rest-auth`?
问题描述
如何在
该页面包含有关您使用哪些api端点的信息可以覆盖
http:// django-rest-auth。 readthedocs.io/en/latest/configuration.html
要登录,您可以使用:
'LOGIN_SERIALIZER':'path.to.custom .LoginSerializer',
为了实现自定义注册逻辑和序列化程序,建议您阅读 http://www.django-rest-framework.org/ 。
1。如文档所示,在您的settings.py中进行设置,
REST_AUTH_SERIALIZERS = {
'LOGIN_SERIALIZER ':'path.to.custom.LoginSerializer',
...
}
2。创建Serailizer类
这是默认的Login Serializer类(复制并粘贴到上面使用的url并根据需要进行编辑)
注意:您还可以使用模型Serializer
class LoginSerializer(serializers .Serializer):
用户名= serializers.CharField(必需= False,allow_blank = True)
电子邮件= serializers.EmailField(required = False,allow_blank = True)
密码= serializers.CharField( style = {'input_type':'password'})
def _validate_email(自己,电子邮件,密码):
用户=无
如果使用电子邮件和密码:
用户=身份验证(电子邮件=电子邮件,密码=密码)
其他:
msg = _(必须包括电子邮件和密码。')
引发异常。 ValidationError(msg)
返回用户
def _validate_username(自己,用户名,密码):
user = None
if用户名和密码:
用户=身份验证(用户名=用户名,密码=密码)
其他:
msg = _(必须包括用户名和密码。')
引发exception.ValidationError(msg)
返回用户
def _validate_username_email(自己,用户名,电子邮件,密码):
user = None
,如果电子邮件和密码:
用户=身份验证(电子邮件=电子邮件,密码=密码)
elif用户名和密码:
用户=身份验证(用户名=用户名,密码=密码)
else:
msg = _(必须包括用户名或电子邮件和密码。')
引发异常。ValidationError(msg)
返回用户
def validate(self,attrs):
用户名= attrs.get('用户名')
电子邮件= attrs.get('email')
密码= attrs.get('密码')
用户=无
,如果设置中为 allauth。INSTALLED_APPS:从allauth.account导入
app_settings
#通过电子邮件进行身份验证
如果app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod。电子邮件:
用户= self._validate_email(电子邮件,密码)
#通过用户名
进行身份验证,如果app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
用户= self._validate_username(用户名,密码)
#通过用户名或电子邮件
进行身份验证其他:
user = self._validate_username_email(用户名,电子邮件,密码)
else:
#如果不使用认证,则不使用allauth
如果电子邮件:
尝试:
username = UserModel.objects.get(email__iexact = email).get_username()
独家t UserModel.DoesNotExist:
通过
如果用户名:
user = self._validate_username_email(username,'',password)
#我们得到了吗支持活跃用户?
如果用户:
如果不是user.is_active:
msg = _('用户帐户被禁用。')
引发异常.ValidationError(msg)
否则:
msg = _('无法使用提供的凭据登录。')
引发异常。ValidationError(msg)
#如果需要,是否已验证电子邮件?
,如果设置中为 rest_auth.registration。INSTALLED_APPS:从allauth.account导入
app_settings
如果app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
email_address = user.emailaddress_set .get(email = user.email)
如果不是email_address.verified:
引发serializers.ValidationError(_('未验证电子邮件。'))
attrs ['用户'] =用户
返回属性
How can I do my logic in http://127.0.0.1:8000/rest-auth/login/
of django-rest-auth
When I login it is like below and the url is provide by django-rest-auth.
How can I write my own logic when I login?
That page have information about which api endpoints you can override http://django-rest-auth.readthedocs.io/en/latest/configuration.html
for login you can use: 'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer', For realize custom registration logic and serializer I'm recommend to you read documentation of http://www.django-rest-framework.org/.
1. set it in your settings.py as shown in the docs,
REST_AUTH_SERIALIZERS = {
'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',
...
}
2. Create Serailizer class
This is default Login Serializer class (copy and paste to url used above and edit as you want )
Note: You can use model Serializer also
class LoginSerializer(serializers.Serializer):
username = serializers.CharField(required=False, allow_blank=True)
email = serializers.EmailField(required=False, allow_blank=True)
password = serializers.CharField(style={'input_type': 'password'})
def _validate_email(self, email, password):
user = None
if email and password:
user = authenticate(email=email, password=password)
else:
msg = _('Must include "email" and "password".')
raise exceptions.ValidationError(msg)
return user
def _validate_username(self, username, password):
user = None
if username and password:
user = authenticate(username=username, password=password)
else:
msg = _('Must include "username" and "password".')
raise exceptions.ValidationError(msg)
return user
def _validate_username_email(self, username, email, password):
user = None
if email and password:
user = authenticate(email=email, password=password)
elif username and password:
user = authenticate(username=username, password=password)
else:
msg = _('Must include either "username" or "email" and "password".')
raise exceptions.ValidationError(msg)
return user
def validate(self, attrs):
username = attrs.get('username')
email = attrs.get('email')
password = attrs.get('password')
user = None
if 'allauth' in settings.INSTALLED_APPS:
from allauth.account import app_settings
# Authentication through email
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
user = self._validate_email(email, password)
# Authentication through username
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
user = self._validate_username(username, password)
# Authentication through either username or email
else:
user = self._validate_username_email(username, email, password)
else:
# Authentication without using allauth
if email:
try:
username = UserModel.objects.get(email__iexact=email).get_username()
except UserModel.DoesNotExist:
pass
if username:
user = self._validate_username_email(username, '', password)
# Did we get back an active user?
if user:
if not user.is_active:
msg = _('User account is disabled.')
raise exceptions.ValidationError(msg)
else:
msg = _('Unable to log in with provided credentials.')
raise exceptions.ValidationError(msg)
# If required, is the email verified?
if 'rest_auth.registration' in settings.INSTALLED_APPS:
from allauth.account import app_settings
if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
email_address = user.emailaddress_set.get(email=user.email)
if not email_address.verified:
raise serializers.ValidationError(_('E-mail is not verified.'))
attrs['user'] = user
return attrs
这篇关于如何在django-rest-auth的http://127.0.0.1:8000/rest-auth/login/中执行我的逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!