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

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