from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from Auth.models import Profile
from N3wtSchool import bdd

class EmailBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):

        if username is None:
            username = kwargs.get(Profile.USERNAME_FIELD)

        try:
            user = Profile.objects.get(email=username)

            # Vérifie le mot de passe de l'utilisateur
            if user.check_password(password):
                return user
        except Profile.DoesNotExist:
            return None

