Search code examples
pythonpython-3.xdjangodjango-authenticationdjango-login

Django - Login with Email


I want django to authenticate users via email, not via usernames. One way can be providing email value as username value, but I dont want that. Reason being, I've a url /profile/<username>/, hence I cannot have a url /profile/[email protected]/.

Another reason being that all emails are unique, but it happen sometimes that the username is already being taken. Hence I'm auto-creating the username as fullName_ID.

How can I just change let Django authenticate with email?

This is how I create a user.

username = `abcd28`
user_email = `[email protected]`
user = User.objects.create_user(username, user_email, user_pass)

This is how I login.

email = request.POST['email']
password = request.POST['password']
username = User.objects.get(email=email.lower()).username
user = authenticate(username=username, password=password)
login(request, user)

Is there any other of of login apart from getting the username first?


Solution

  • You should write a custom authentication backend. Something like this will work:

    from django.contrib.auth import get_user_model
    from django.contrib.auth.backends import ModelBackend
    
    class EmailBackend(ModelBackend):
        def authenticate(self, request, username=None, password=None, **kwargs):
            UserModel = get_user_model()
            try:
                user = UserModel.objects.get(email=username)
            except UserModel.DoesNotExist:
                return None
            else:
                if user.check_password(password):
                    return user
            return None
    

    Then, set that backend as your auth backend in your settings:

    AUTHENTICATION_BACKENDS = ['path.to.auth.module.EmailBackend']
    

    Updated. Inherit from ModelBackend as it implements methods like get_user() already.

    See docs here: https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#writing-an-authentication-backend