<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.hashers import check_password
from .models import Users
from django.utils import timezone
import re
# documents/backends.py

class EmailPhoneBackend(BaseBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        print(f"BACKEND AUTH: username='{username}', password='{password}'")
        
        if not username or not password:
            return None
            
        try:
            user = self._find_user_by_identifier(username)
            print(f"BACKEND: User found: {user}")
            
            if user:
                print(f"BACKEND: User password hash: {user.password}")
                print(f"BACKEND: Password check: {check_password(password, user.password)}")
                
                if user.is_active and check_password(password, user.password):
                    user.last_login = timezone.now()
                    user.save(update_fields=['last_login'])
                    print("BACKEND: Authentication SUCCESS")
                    return user
            
            print("BACKEND: Authentication FAILED")
            return None
                
        except Exception as e:
            print(f"BACKEND ERROR: {e}")
            return None
    
    def _find_user_by_identifier(self, identifier):
        """Trouve un utilisateur par email ou téléphone"""
        identifier = identifier.strip()
        print(f"BACKEND: Searching for identifier: '{identifier}'")
        
        # Chercher par téléphone exact
        user = Users.objects.filter(phone=identifier).first()
        if user:
            print(f"BACKEND: Found by exact phone: {user.phone}")
            return user
        
        # Chercher par téléphone (nettoyer et chercher)
        phone_clean = re.sub(r'[^\d+]', '', identifier)
        if phone_clean:
            user = Users.objects.filter(phone=phone_clean).first()
            if user:
                print(f"BACKEND: Found by cleaned phone: {user.phone}")
                return user
        
        # Chercher par téléphone partiel
        if phone_clean:
            # Si le numéro commence par +237, essayer sans
            if phone_clean.startswith('+237'):
                phone_without_code = phone_clean[4:]  # Enlève +237
                user = Users.objects.filter(phone__endswith=phone_without_code).first()
                if user:
                    print(f"BACKEND: Found by phone without code: {user.phone}")
                    return user
            
            # Essayer avec les derniers chiffres
            if len(phone_clean) > 8:
                last_digits = phone_clean[-9:]  # Derniers 9 chiffres
                user = Users.objects.filter(phone__endswith=last_digits).first()
                if user:
                  