Django Authentication – How to build Login/Logout/Signup for custom User
Building user authentication is not easy, in almost case, it’s complicated. Fortunately, Django has a powerful built-in User authentication that helps us create our Authentication system fast. By default, the User model in Django auth
app contains fields: username, password, email, first_name, last_name… However, using our own custom user model allows us deal with user profile more comfortably. For example, what if we want to add more fields: full_name or age?
In this tutorial, we’re gonna look at way to customize authentication in Django (version 2.1) using subclass of AbstractBaseUser
: AbstractUser
. All User authentication data will be stored in MySQL/PostgreSQL database that we’ll show you how to config the datasource.
It will be very interesting. Let’s go through the steps.
We will code our custom signup()
function, login()
and logout()
is automatically implemented by Django auth
.
All User data will be saved in MySQL/PostgreSQL database.
django-admin startproject DjangoAuth
Run following commands to create new Django App named authen inside the project:
cd DjangoAuth
python manage.py startapp authen
Open upload/apps.py, we can see AuthenConfig
class (subclass of the django.apps.AppConfig
) that represents our Django app and its configuration:
from django.apps import AppConfig
class AuthenConfig(AppConfig):
name = 'authen'
Open settings.py, find INSTALLED_APPS
, then add:
INSTALLED_APPS = [
...
'authen.apps.AuthenConfig',
]
pip install pymysql
.
Once the installation is successful, import this module in DjangoAuth/init.py:
Django Authentication – How to build Login/Logout/Signup for custom User
#django #authentication #login #logout #singup