Skip to content

Commit

Permalink
Fix login with e-mail not working
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigocam committed Nov 21, 2024
1 parent 2415fad commit 075df89
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,33 @@ def __init__(self, *args, **kwargs):
self.fields["password"].widget.attrs["placeholder"] = _("password")

def clean(self):
cleaned_data = super(LoginForm, self).clean()

username_or_email = cleaned_data.get("username", "")
username_or_email = self.cleaned_data.get("username", "")
search_by = {}

if "@" in username_or_email:
search_by["email"] = username_or_email
else:
search_by["username"] = username_or_email

user = User.objects.get(**search_by)
if not user:
raise forms.ValidationError(_("Username/email not found"))
try:
user = User.objects.get(**search_by)
except User.DoesNotExist as e:
raise forms.ValidationError(_("Username/email not found")) from e

cleaned_data["username"] = user.username
self.cleaned_data["username"] = user.username

password = cleaned_data.get("password")
password = self.cleaned_data.get("password")

logged_user = authenticate(username=user.username, password=password)
if not logged_user:
logged_user = authenticate(self.request, username=user.username, password=password)
if logged_user is None:
raise forms.ValidationError(_("Wrong password!"))

self.confirm_login_allowed(logged_user)
self.user = logged_user

return self.cleaned_data

return cleaned_data
def get_user(self):
return getattr(self, "user", None)


class UploadMarkerForm(forms.ModelForm):
Expand Down

0 comments on commit 075df89

Please sign in to comment.