from typing import Optional

from accounts.models import AccountModel
from django.contrib.auth.backends import ModelBackend


class EmailAuthenticationBackend(ModelBackend):
    def authenticate(self, request, **kwargs) -> Optional[AccountModel]:
        email = kwargs.get("email")
        password = kwargs.get("password")
        if not email or not password:
            return None
        try:
            account: AccountModel = AccountModel.objects.get(email=email)
        except AccountModel.DoesNotExist:
            return None

        if account.check_password(password) and self.user_can_authenticate(
            account
        ):
            return account
        else:
            return None

from typing import Optional

from accounts.models import AccountModel
from django.contrib.auth.backends import ModelBackend


class EmailAuthenticationBackend(ModelBackend):
    def authenticate(self, request, **kwargs) -> Optional[AccountModel]:
        email = kwargs.get("email")
        password = kwargs.get("password")
        if not email or not password:
            return None
        try:
            account: AccountModel = AccountModel.objects.get(email=email)
        except AccountModel.DoesNotExist:
            return None

        if account.check_password(password) and self.user_can_authenticate(
            account
        ):
            return account
        else:
            return None
