Customizing Phone Number Authentication in Django
As we know, Introducing phone number-based authentication in the Django Admin Panel is a strategic move to modernize and enhance your web application’s security. In the traditional Django authentication system, users use a unique username and password to access their accounts. However, in today’s digital landscape, where mobile phones are integral, implementing phone number authentication streamlines and secures the login process. This change aligns your application with current user preferences and strengthens security by reducing password-related risks. In this guide, we’ll explore the key steps to transition from username-based to phone number-based authentication in the Django Admin Panel, providing a more convenient and secure user experience.
Django Admin Panel Username Authentication with Phone Number Authentication
To install Django follow these steps.
Starting the Project Folder
To start the project use this command
django-admin startproject newton
cd newton
To start the app use this command
python manage.py startapp accounts
File Structure :
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"accounts", //App name
]
Setting up necessary files
models.py: Here we are creating the models so that we can store information in our database. We define a custom User model that inherits from AbstractUser. This allows you to extend the default user model provided by Django.
from django.db import models
from django.contrib.auth.models import AbstractUser
from .manager import UserManager
class GFG(AbstractUser):
phone = models.CharField(max_length=12, unique=True)
USERNAME_FIELD = 'phone'
REQUIRED_FIELDS = []
objects = UserManager()
manager.py: We import BaseUserManager from django.contrib.auth.base_user to create a custom user manager.We define a custom UserManager class that extends BaseUserManager. This manager is responsible for creating and managing user instances.The create_user method is used to create a standard user with a phone number, an optional password, and any extra fields passed as keyword arguments. It checks if a phone number is provided and raises a ValueError if not.
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
use_in_migrations = True
def create(self, phone, password=None, **extra_fields):
if not phone:
raise ValueError('Phone number is required')
user = self.model(phone=phone, **extra_fields)
user.set_password(password)
user.save()
return user
def superuser(self, phone, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
return self.create(phone, password, **extra_fields)
admin.py:Here we are registering the models.
from django.contrib import admin
from .models import GFG
admin.site.register(GFG)
settings.py:Here we are adding the command to make changes in authentication process of Django.
AUTH_USER_MODEL = "accounts.GFG" #Appname.model(classname)
Deployement of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output
Conclusion :
Changing Django Admin Panel Username Authentication to Phone Number Authentication is a significant modification to the default authentication system. This alteration involves customizing Django’s authentication backend, forms, and views to accommodate phone numbers as the primary identifier for administrators.
In conclusion, implementing phone number authentication in the Django Admin Panel enhances security and user-friendliness. However, it requires careful consideration of user privacy, validation, and potential challenges related to international phone number formats and verification methods. Ensure you follow best practices and consider regulatory requirements when implementing such changes.
Contact Us