Skip to content

Commit 54eb54a

Browse files
committed
Cleaned up LoginSerializer codebase
1 parent b12ed79 commit 54eb54a

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

docs/api_endpoints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Basic
77
- /rest-auth/login/ (POST)
88

99
- username (string)
10+
- email (string)
1011
- password (string)
1112

1213

rest_auth/serializers.py

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,73 @@ class LoginSerializer(serializers.Serializer):
1919
email = serializers.EmailField(required=False, allow_blank=True)
2020
password = serializers.CharField(style={'input_type': 'password'})
2121

22+
def _validate_email(self, email, password):
23+
user = None
24+
25+
if email and password:
26+
user = authenticate(email=email, password=password)
27+
else:
28+
msg = _('Must include "email" and "password".')
29+
raise exceptions.ValidationError(msg)
30+
31+
return user
32+
33+
def _validate_username(self, username, password):
34+
user = None
35+
36+
if username and password:
37+
user = authenticate(username=username, password=password)
38+
else:
39+
msg = _('Must include "username" and "password".')
40+
raise exceptions.ValidationError(msg)
41+
42+
return user
43+
44+
def _validate_username_email(self, username, email, password):
45+
user = None
46+
47+
if email and password:
48+
user = authenticate(email=email, password=password)
49+
elif username and password:
50+
user = authenticate(username=username, password=password)
51+
else:
52+
msg = _('Must include either "username" or "email" and "password".')
53+
raise exceptions.ValidationError(msg)
54+
55+
return user
56+
2257
def validate(self, attrs):
2358
username = attrs.get('username')
2459
email = attrs.get('email')
2560
password = attrs.get('password')
2661

62+
user = None
63+
2764
if 'allauth' in settings.INSTALLED_APPS:
2865
from allauth.account import app_settings
66+
2967
# Authentication through email
3068
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
31-
if email and password:
32-
user = authenticate(email=email, password=password)
33-
else:
34-
msg = _('Must include "email" and "password".')
35-
raise exceptions.ValidationError(msg)
69+
user = self._validate_email(email, password)
70+
3671
# Authentication through username
37-
elif app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
38-
if username and password:
39-
user = authenticate(username=username, password=password)
40-
else:
41-
msg = _('Must include "username" and "password".')
42-
raise exceptions.ValidationError(msg)
72+
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
73+
user = self._validate_username(username, password)
74+
4375
# Authentication through either username or email
4476
else:
45-
if email and password:
46-
user = authenticate(email=email, password=password)
47-
elif username and password:
48-
user = authenticate(username=username, password=password)
49-
else:
50-
msg = _('Must include either "username" or "email" and "password".')
51-
raise exceptions.ValidationError(msg)
52-
53-
elif username or email and password:
54-
# Try get username if we have in request email
55-
if email and not username:
77+
user = self._validate_username_email(username, email, password)
78+
79+
else:
80+
# Authentication without using allauth
81+
if email:
5682
try:
5783
username = UserModel.objects.get(email__iexact=email).username
5884
except UserModel.DoesNotExist:
59-
user = None
60-
if username:
61-
user = authenticate(username=username, password=password)
85+
pass
6286

63-
else:
64-
msg = _('Must include either "username" or "email" and "password".')
65-
raise exceptions.ValidationError(msg)
87+
if username:
88+
user = self._validate_username_email(username, '', password)
6689

6790
# Did we get back an active user?
6891
if user:

0 commit comments

Comments
 (0)