From c19b01ffa02b3bf7b655a51f89e67fc8824e297a Mon Sep 17 00:00:00 2001 From: Jorran de Wit <jorrandewit@outlook.com> Date: Mon, 14 Aug 2017 21:32:41 +0200 Subject: [PATCH] Add login with email functionality Now users will be able to log in with their email as well as their username. I make it implicit; I do not tell the user this in the login form, as I'm not sure if the non-uniqueness of the `email` field could cause any problems. I now built a rude try-catch to ease the login process for the user instead. --- scipost/forms.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scipost/forms.py b/scipost/forms.py index 8273339f4..ba8274e92 100644 --- a/scipost/forms.py +++ b/scipost/forms.py @@ -307,7 +307,17 @@ class AuthenticationForm(forms.Form): """ username = self.cleaned_data['username'] password = self.cleaned_data['password'] - return authenticate(username=username, password=password) + user = authenticate(username=username, password=password) + if user: + return user + + try: + _user = User.objects.get(email=username) + return authenticate(username=_user.username, password=password) + except: + # Catch all exceptions. This method should be upgraded in the next Django + # update anyway and not a single exception should propagate to the user, never! + return None def get_redirect_url(self, request): """ -- GitLab