mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-02-06 21:20:32 +03:00
Cleaned up LoginSerializer codebase
This commit is contained in:
parent
b12ed79bb1
commit
54eb54ad65
|
@ -7,6 +7,7 @@ Basic
|
||||||
- /rest-auth/login/ (POST)
|
- /rest-auth/login/ (POST)
|
||||||
|
|
||||||
- username (string)
|
- username (string)
|
||||||
|
- email (string)
|
||||||
- password (string)
|
- password (string)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -19,50 +19,73 @@ class LoginSerializer(serializers.Serializer):
|
||||||
email = serializers.EmailField(required=False, allow_blank=True)
|
email = serializers.EmailField(required=False, allow_blank=True)
|
||||||
password = serializers.CharField(style={'input_type': 'password'})
|
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):
|
def validate(self, attrs):
|
||||||
username = attrs.get('username')
|
username = attrs.get('username')
|
||||||
email = attrs.get('email')
|
email = attrs.get('email')
|
||||||
password = attrs.get('password')
|
password = attrs.get('password')
|
||||||
|
|
||||||
|
user = None
|
||||||
|
|
||||||
if 'allauth' in settings.INSTALLED_APPS:
|
if 'allauth' in settings.INSTALLED_APPS:
|
||||||
from allauth.account import app_settings
|
from allauth.account import app_settings
|
||||||
|
|
||||||
# Authentication through email
|
# Authentication through email
|
||||||
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
|
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
|
||||||
if email and password:
|
user = self._validate_email(email, password)
|
||||||
user = authenticate(email=email, password=password)
|
|
||||||
else:
|
|
||||||
msg = _('Must include "email" and "password".')
|
|
||||||
raise exceptions.ValidationError(msg)
|
|
||||||
# Authentication through username
|
# Authentication through username
|
||||||
elif app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
|
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
|
||||||
if username and password:
|
user = self._validate_username(username, password)
|
||||||
user = authenticate(username=username, password=password)
|
|
||||||
else:
|
|
||||||
msg = _('Must include "username" and "password".')
|
|
||||||
raise exceptions.ValidationError(msg)
|
|
||||||
# Authentication through either username or email
|
# Authentication through either username or email
|
||||||
else:
|
else:
|
||||||
if email and password:
|
user = self._validate_username_email(username, email, 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)
|
|
||||||
|
|
||||||
elif username or email and password:
|
else:
|
||||||
# Try get username if we have in request email
|
# Authentication without using allauth
|
||||||
if email and not username:
|
if email:
|
||||||
try:
|
try:
|
||||||
username = UserModel.objects.get(email__iexact=email).username
|
username = UserModel.objects.get(email__iexact=email).username
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
user = None
|
pass
|
||||||
if username:
|
|
||||||
user = authenticate(username=username, password=password)
|
|
||||||
|
|
||||||
else:
|
if username:
|
||||||
msg = _('Must include either "username" or "email" and "password".')
|
user = self._validate_username_email(username, '', password)
|
||||||
raise exceptions.ValidationError(msg)
|
|
||||||
|
|
||||||
# Did we get back an active user?
|
# Did we get back an active user?
|
||||||
if user:
|
if user:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user