Skip to content

Commit

Permalink
Fix broken login for user not found
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigocam committed Nov 19, 2024
1 parent f59a065 commit e35cd38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
49 changes: 18 additions & 31 deletions src/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,45 +142,32 @@ def __init__(self, *args, **kwargs):
self.fields["username"].widget.attrs["placeholder"] = _("username / email")
self.fields["password"].widget.attrs["placeholder"] = _("password")

def clean_username(self):
username_or_email = self.cleaned_data.get("username")
def clean(self):
cleaned_data = super(LoginForm, self).clean()

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

if "@" in username_or_email:
if not User.objects.filter(email=username_or_email).exists():
raise forms.ValidationError(_("Username/email not found"))
user = User.objects.get(email=username_or_email)
if user:
return user.username
search_by["email"] = username_or_email
else:
if not User.objects.filter(username=username_or_email).exists():
raise forms.ValidationError(_("Username/email not found"))
search_by["username"] = username_or_email

# Already is a valid username
return username_or_email
user = User.objects.get(**search_by)
if not user:
raise forms.ValidationError(_("Username/email not found"))

def clean_password(self):
password = self.cleaned_data.get("password")
username_or_email = self.cleaned_data.get("username")
user = None
username_or_email_wrong = False
cleaned_data["username"] = user.username

if "@" in username_or_email:
if User.objects.filter(email=username_or_email).exists():
username = User.objects.get(email=username_or_email).username
user = authenticate(username=username, password=password)
else:
username_or_email_wrong = True
# raise forms.ValidationError(_('Email Wrong!'))
else:
if User.objects.filter(username=username_or_email).exists():
user = authenticate(username=username_or_email, password=password)
else:
username_or_email_wrong = True
# raise forms.ValidationError(_('Username Wrong!'))
password = cleaned_data.get("password")

if not user and not username_or_email_wrong:
logged_user = authenticate(username=user.username, password=password)
if not logged_user:
raise forms.ValidationError(_("Wrong password!"))

return password
self.confirm_login_allowed(logged_user)

return cleaned_data


class RecoverPasswordForm(forms.Form):
Expand Down
8 changes: 7 additions & 1 deletion src/users/jinja2/users/login.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
<p class="login-field {{field.name}}">
{{ field }}
</p>
<h4 style="color:red">{{ field.errors }}</h4>
{% endfor%}
{% if form.non_field_errors() %}
<ul class="errorlist">
{% for error in form.non_field_errors() %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<div class="form-options">
<p>
<input id="remember-me-chk" type="checkbox" name="remember" value="1">
Expand Down

0 comments on commit e35cd38

Please sign in to comment.