Add email to authentication

This commit is contained in:
cuom1999 2023-11-27 19:49:38 -06:00
parent 26f26a1722
commit 39b42a29a4
5 changed files with 241 additions and 221 deletions

View file

@ -430,7 +430,7 @@ AUTHENTICATION_BACKENDS = (
"social_core.backends.google.GoogleOAuth2", "social_core.backends.google.GoogleOAuth2",
"social_core.backends.facebook.FacebookOAuth2", "social_core.backends.facebook.FacebookOAuth2",
"judge.social_auth.GitHubSecureEmailOAuth2", "judge.social_auth.GitHubSecureEmailOAuth2",
"django.contrib.auth.backends.ModelBackend", "judge.authentication.CustomModelBackend",
) )
SOCIAL_AUTH_PIPELINE = ( SOCIAL_AUTH_PIPELINE = (

15
judge/authentication.py Normal file
View file

@ -0,0 +1,15 @@
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class CustomModelBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
# Check if the username is an email
user = User.objects.get(username=username)
except User.DoesNotExist:
# If the username is not an email, try authenticating with the username field
user = User.objects.filter(email=username).first()
if user and user.check_password(password):
return user

View file

@ -418,7 +418,9 @@ class NewMessageForm(ModelForm):
class CustomAuthenticationForm(AuthenticationForm): class CustomAuthenticationForm(AuthenticationForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(CustomAuthenticationForm, self).__init__(*args, **kwargs) super(CustomAuthenticationForm, self).__init__(*args, **kwargs)
self.fields["username"].widget.attrs.update({"placeholder": _("Username")}) self.fields["username"].widget.attrs.update(
{"placeholder": _("Username/Email")}
)
self.fields["password"].widget.attrs.update({"placeholder": _("Password")}) self.fields["password"].widget.attrs.update({"placeholder": _("Password")})
self.has_google_auth = self._has_social_auth("GOOGLE_OAUTH2") self.has_google_auth = self._has_social_auth("GOOGLE_OAUTH2")

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@
{% csrf_token %} {% csrf_token %}
{% if form.errors %} {% if form.errors %}
<div id="form-errors"> <div id="form-errors">
<p class="error">{{ _('Invalid username or password.') }}</p> <p class="error">{{ _('Invalid username/email or password.') }}</p>
</div> </div>
{% endif %} {% endif %}
<table border="0" style="text-align:left"> <table border="0" style="text-align:left">