Skip to main content

Enables developers to implement login with email or username or any other field on your user model in django

Project description

PyPi Status Documentation Status

Django Multiple Authentication allows you to use either email or username field or any other field on your user model for your user authentication.

Source code

https://github.com/KoredeDavid/django-multiple-authentication/

Documentation

https://django-multiple-authentication.readthedocs.io/en/latest/


Rationale

Django’s default authentication only accepts username for user authentication. So the package allows you to use either email or username or any other stuff on your user table for user authentication. It works with django’s in-built authentication function, so it works as long as django’s authentication function is called.

Requirements

  • Python >= 3.6

  • Django (3.0, 3.1, 3.2, 4.0, 4.1)

These are the officially supported python and django package versions. Other versions will probably work.

Installation

Django Multiple Authentication can be installed with pip:

pip install django
pip install django-multiple-authentication

Project Configuration

Add multiple_auth to your list of INSTALLED_APPS in your settings.py :

 INSTALLED_APPS = [
     ...
     "multiple_auth",
]

Now we tell django what AUTHENTICATION_BACKENDS we want to use for user authentication. Update your settings.py with this:

AUTHENTICATION_BACKENDS = (
    'multiple_auth.backends.MultipleAuthentication',
)

Usage & Illustration

Startup up a new project like this if you haven’t

django-admin startproject sampleproject

cd sampleproject

python manage.py makemigrations

python manage.py migrate

Create a superuser

python manage.py createsuperuser --username=test --email=test@email.com

It will bring a prompt to set password. So just set your password and you’re done creating a user.

Now we tell django what AUTHENTICATION_BACKENDS we want to use for user authentication. Update your settings.py with this:

AUTHENTICATION_BACKENDS = (
    'multiple_auth.backends.MultipleAuthentication',
)

Add MULTIPLE_AUTH settings (a dictionary) to your settings.py. Include a key of auth_fields a value of the list of field(s) in your User Model you want to accept for your authentication.

You can use one or more fields. For illustration, we will be using the username and email fields. So update your settings like this:

MULTIPLE_AUTH = {
    'auth_fields': ['username', 'email']
}

You can test it with your login page or your API. It works also on the django-admin panel.

Note that the the auth_fields is not just limited two fields you can have one, two or more fields.

One Field:

MULTIPLE_AUTH = {
    'auth_fields': ['id']
}

Two OR More fields

MULTIPLE_AUTH = {
    'auth_fields': ['email', 'username', 'phone_number', 'id', ...]
}
A GIF showing a user logging in with his ``email``, ``username`` and ``id``.

Here’s a GIF showing a user logging in with his ``email``, ``username`` and ``id``.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-multiple-authentication-2.0.5.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page