NDOJ/judge/authentication.py
2023-11-27 19:49:38 -06:00

15 lines
611 B
Python

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