code: def authenticate(self, username=None, password=None, **kwargs):
          
          try:
             # Try to fetch the user by searching the username or email field
              user = get_user_model().objects.filter(Q(username=username)|Q(email=username))[0]
              if check_password(password, user.password):
                  return user
              else:
                  return None
          except Exception as e:
              # No user was found, return None - triggers default login failed
              return None
docs: Allow users to log in with their email address or username.