How to login in Django admin using email?

Writing an authentication backend

Django allows you to authenticate against different sources. The AUTHENTICATION_
BACKENDS setting includes the list of authentication backends for your project. By
default, this setting is set as follows:

['Django.contrib.auth.backends.ModelBackend']

The default ModelBackend authenticates users against the database using the user
model of Django.contrib.auth .

Let’s start coding.

step 1. Create a project directory.

mkdir custom_auth_backend  && cd custom_auth_backend

step2. Create a virtual environment to isolate package dependency locally.

python -m venv env

Step 3. Activate virtual environment.

.  env/bin/activate

Step4. Install Dango.

pip install Django.

Step5.  Set up a project.

Django-admin start project cs_auth_demo           #You can project the name of your choice.

 

Step6.  Use migrate command so you can use the default Django admin panel.

python manage.py migrate

Step7.  Create a superuser.

python manage.py createsuperuser

 

Now you will be able to login using your username and password but you can’t log in using your email id and password. Try it.

 

 

Your project structure should look something like this.

├── cs_auth_demo
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── authentication.cpython-39.pyc
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
└── manage.py

 

Step8. Create a .py file in your main project directory.

authentication .py

Your project structure should look something like this now.

├── cs_auth_demo
│   ├── asgi.py
│   ├── authentication.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── authentication.cpython-39.pyc
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
└── manage.py

Step9. Add the following code in the authentication. Py file.

from django.conf import settings
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User


class EmailAuthBackend(object):
    """
    Authenticate using an e-mail address.
    """
    def authenticate(self, request, username=None, password=None):
        try:
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user
            return None
       except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

 

If you want to explore more, follow the link below.

https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#other-authenticationsources

Step10. Add the following code to the settings.py file.

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'cs_auth_demo.authentication.EmailAuthBackend',
]

Step11.  Run the server and try logging in using email and password.

python manage.py run server.

 

Happy coding.

 

Leave a Reply