Authentication with email

This commit is contained in:
Usuario 2017-09-24 20:59:06 -05:00
parent e6d1077ae2
commit 602c764a23

View File

@ -25,7 +25,12 @@ class LoginSerializer(serializers.Serializer):
user = None user = None
if email and password: if email and password:
user = authenticate(email=email, password=password) try:
username = UserModel.objects.get(email__iexact=email).get_username()
user = authenticate(username=username, password=password)
except UserModel.DoesNotExist:
msg = _('Unable to log in with provided credentials.')
raise exceptions.ValidationError(msg)
else: else:
msg = _('Must include "email" and "password".') msg = _('Must include "email" and "password".')
raise exceptions.ValidationError(msg) raise exceptions.ValidationError(msg)
@ -47,7 +52,12 @@ class LoginSerializer(serializers.Serializer):
user = None user = None
if email and password: if email and password:
user = authenticate(email=email, password=password) try:
username = UserModel.objects.get(email__iexact=email).get_username()
user = authenticate(username=username, password=password)
except UserModel.DoesNotExist:
msg = _('Unable to log in with provided credentials.')
raise exceptions.ValidationError(msg)
elif username and password: elif username and password:
user = authenticate(username=username, password=password) user = authenticate(username=username, password=password)
else: else: