from django.contrib.auth.backends import ModelBackend
from django.core.validators import email_re
from django.contrib.auth import models, load_backend, login
from django.conf import settings


class EmailBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        if email_re.search(username):
            try:
                user = models.User.objects.get(email=username)
                if user.check_password(password):
                    return user
            except models.User.DoesNotExist:
                return None
        return None


def login_user(request, user):
    if not hasattr(user, 'backend'):
        for backend in settings.AUTHENTICATION_BACKENDS:
            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
    if hasattr(user, 'backend'):
        return login(request, user)

from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.contrib.auth import models, load_backend, login
from django.conf import settings


class EmailBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            validate_email(username)
        except ValidationError:
            return None
        try:
            user = models.User.objects.get(email=username)
            if user.check_password(password):
                return user
        except models.User.DoesNotExist:
            return None
        return None


def login_user(request, user):
    if not hasattr(user, 'backend'):
        for backend in settings.AUTHENTICATION_BACKENDS:
            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
    if hasattr(user, 'backend'):
        return login(request, user)

Validate email the correct way
Validate email the correct way
  from django.contrib.auth.backends import ModelBackend
+ from django.core.exceptions import ValidationError
- from django.core.validators import email_re
+ from django.core.validators import validate_email
  from django.contrib.auth import models, load_backend, login
  from django.conf import settings
  
  
  class EmailBackend(ModelBackend):
      def authenticate(self, username=None, password=None):
-         if email_re.search(username):
-             try:
+         try:
+             validate_email(username)
+         except ValidationError:
+             return None
+         try:
-                 user = models.User.objects.get(email=username)
+             user = models.User.objects.get(email=username)
-                 if user.check_password(password):
+             if user.check_password(password):
-                     return user
+                 return user
-             except models.User.DoesNotExist:
+         except models.User.DoesNotExist:
-                 return None
+             return None
          return None
  
  
  def login_user(request, user):
      if not hasattr(user, 'backend'):
          for backend in settings.AUTHENTICATION_BACKENDS:
              if user == load_backend(backend).get_user(user.pk):
                  user.backend = backend
                  break
      if hasattr(user, 'backend'):
          return login(request, user)
  
Validate email the correct way
## Code Before:
from django.contrib.auth.backends import ModelBackend
from django.core.validators import email_re
from django.contrib.auth import models, load_backend, login
from django.conf import settings


class EmailBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        if email_re.search(username):
            try:
                user = models.User.objects.get(email=username)
                if user.check_password(password):
                    return user
            except models.User.DoesNotExist:
                return None
        return None


def login_user(request, user):
    if not hasattr(user, 'backend'):
        for backend in settings.AUTHENTICATION_BACKENDS:
            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
    if hasattr(user, 'backend'):
        return login(request, user)

## Instruction:
Validate email the correct way
## Code After:
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.contrib.auth import models, load_backend, login
from django.conf import settings


class EmailBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            validate_email(username)
        except ValidationError:
            return None
        try:
            user = models.User.objects.get(email=username)
            if user.check_password(password):
                return user
        except models.User.DoesNotExist:
            return None
        return None


def login_user(request, user):
    if not hasattr(user, 'backend'):
        for backend in settings.AUTHENTICATION_BACKENDS:
            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
    if hasattr(user, 'backend'):
        return login(request, user)
