Skip to content

Commit bb2fb65

Browse files
committed
Auth by email
1 parent 56773d8 commit bb2fb65

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

rest_auth/serializers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ def validate(self, attrs):
5050
msg = _('Must include either "username" or "email" and "password".')
5151
raise exceptions.ValidationError(msg)
5252

53-
elif username and password:
54-
user = authenticate(username=username, password=password)
53+
elif username or email and password:
54+
# Try get username if we have in request email
55+
if email and not username:
56+
try:
57+
username = UserModel.objects.get(email__iexact=email).username
58+
except UserModel.DoesNotExist:
59+
user = None
60+
if username:
61+
user = authenticate(username=username, password=password)
5562

5663
else:
57-
msg = _('Must include "username" and "password".')
64+
msg = _('Must include either "username" or "email" and "password".')
5865
raise exceptions.ValidationError(msg)
5966

6067
# Did we get back an active user?

rest_auth/tests/test_api.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.test import TestCase
33
from django.contrib.auth import get_user_model
44
from django.core import mail
5+
from django.conf import settings
56
from django.test.utils import override_settings
67
from django.utils.encoding import force_text
78

@@ -90,6 +91,51 @@ def test_login(self):
9091
# test empty payload
9192
self.post(self.login_url, data={}, status_code=400)
9293

94+
def test_login_by_email(self):
95+
# starting test without allauth app
96+
settings.INSTALLED_APPS.remove('allauth')
97+
98+
payload = {
99+
"email": self.EMAIL.lower(),
100+
"password": self.PASS
101+
}
102+
# there is no users in db so it should throw error (400)
103+
self.post(self.login_url, data=payload, status_code=400)
104+
105+
self.post(self.password_change_url, status_code=403)
106+
107+
# create user
108+
user = get_user_model().objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
109+
110+
# test auth by email
111+
self.post(self.login_url, data=payload, status_code=200)
112+
self.assertEqual('key' in self.response.json.keys(), True)
113+
self.token = self.response.json['key']
114+
115+
# test auth by email in different case
116+
payload = {
117+
"email": self.EMAIL.upper(),
118+
"password": self.PASS
119+
}
120+
self.post(self.login_url, data=payload, status_code=200)
121+
self.assertEqual('key' in self.response.json.keys(), True)
122+
self.token = self.response.json['key']
123+
124+
# test inactive user
125+
user.is_active = False
126+
user.save()
127+
self.post(self.login_url, data=payload, status_code=400)
128+
129+
# test wrong email/password
130+
payload = {
131+
"email": 't' + self.EMAIL,
132+
"password": self.PASS
133+
}
134+
self.post(self.login_url, data=payload, status_code=400)
135+
136+
# test empty payload
137+
self.post(self.login_url, data={}, status_code=400)
138+
93139
def test_password_change(self):
94140
login_payload = {
95141
"username": self.USERNAME,

0 commit comments

Comments
 (0)