Django Authentication – How to build Login/Logout/Signup for custom User
Tags: django, authentication
Published: 2021-04-20 14:51:16

https://grokonez.com/django/django-authentication-login-example-logout-signup-for-custom-user-tutorial

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 <a href="https://docs.djangoproject.com/en/2.1/topics/auth/" target="_blank" rel="nofollow noopener noreferrer">User authentication</a> that helps us create our Authentication system fast. By default, the User model in Django <code>auth</code> app contains fields: <em>username</em>, <em>password</em>, <em>email</em>, <em>first_name</em>, <em>last_name</em>... 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: <em>full_name</em> or <em>age</em>?

In this tutorial, we're gonna look at way to customize authentication in Django (version 2.1) using subclass of <code>AbstractBaseUser</code>: <code>AbstractUser</code>. All User authentication data will be stored in <strong>MySQL</strong>/<strong>PostgreSQL</strong> database that we'll show you how to config the datasource.

It will be very interesting. Let's go through the steps.

<!--more-->
<h2>Django Custom Authentication Project overview</h2>
<h3>Goal</h3>
We will build a Dajngo Project with Authentication app that has login/logout/signup with custom fields such as full name and age:

<img src="https://grokonez.com/wp-content/uploads/2019/03/django-authentication-example-signup-login-logout-project-goal.png" alt="django-authentication-example-signup-login-logout-project-goal" width="565" height="551" class="alignnone size-full wp-image-17533" />

We will code our custom <code>signup()</code> function, <code>login()</code> and <code>logout()</code> is automatically implemented by Django <code>auth</code>.

All User data will be saved in MySQL/PostgreSQL database.
<h3>Project Structure</h3>
Here is the folders and files structure that we will create in the next steps.

<img src="https://grokonez.com/wp-content/uploads/2019/03/django-authentication-example-signup-login-logout-project-structure.png" alt="django-authentication-example-signup-login-logout-project-structure" width="238" height="381" class="alignnone size-full wp-image-17531" />
<h2>Setup Django Custom Authentication Project</h2>
Create Django project named <strong>DjangoAuth</strong> with command:
<code>django-admin startproject DjangoAuth</code>

Run following commands to create new Django App named <strong>authen</strong> inside the project:
- <code>cd DjangoAuth</code>
- <code>python manage.py startapp authen</code>

Open <strong><em>upload/apps.py</em></strong>, we can see <code>AuthenConfig</code> class (subclass of the <code>django.apps.AppConfig</code>) that represents our Django app and its configuration:
<pre><code class="language-python">
from django.apps import AppConfig


class AuthenConfig(AppConfig):
    name = 'authen'
</code></pre>
Open <strong><em>settings.py</em></strong>, find <code>INSTALLED_APPS</code>, then add:
<pre><code class="language-python">
INSTALLED_APPS = [
    ...
    'authen.apps.AuthenConfig',
]
</code></pre>
<h2>Config Django project to work with database</h2>
<h3>MySQL Database</h3>
<h4>Install & Import Python MySQL Client</h4>
We have to install Python MySQL Client to work with MySQL database.
In this tutorial, we use <strong>pymysql</strong>: <code>pip install pymysql</code>.

Once the installation is successful, import this module in <strong><em>DjangoAuth/__init__.py</em></strong>:


https://grokonez.com/django/django-authentication-login-example-logout-signup-for-custom-user-tutorial

Django Authentication – How to build Login/Logout/Signup for custom User