rx-36

Creating two google sign-in for each user type using django-allauth Part 1

Note: On wide screens, code blocks may appear beside the explanatory text; on narrower screens (tablet/mobile) they appear below. Instructions like “add the code” refer to the adjacent code block regardless of placement.

Hello. Django developers have probably used a package called “Allauth” at least once. Allauth is the most well-known authentication system among Django's third-party packages. When building an application with Django, many developers might first consider using django-allauth for authentication.

django-allauth is very useful because it simplifies the process of creating an authentication system. However, there is little information available online about how to create an authentication system with multiple user types and registration/login forms using Allauth. Furthermore, add to this multiple user-type logins, plus social account authentication(such as google login), and this practice becomes even more difficult to find on the Internet.

In this article, I will describe how to create an authentication system using Allauth that integrates multiple user types, multiple login screens, and authentication with Google login. Although there are still some issues to be solved, I believe I have succeeded in creating a working solution.

This article is quite lengthy! Here is the sequence of steps involved in this process:

1. Application specification

2. Django Basic Setup

3. django-allauth setup

4. Creating Three Types of Models

5. Creating a User Manager

6. Adding a new parameter for the user type (this is the most crucial part)

7. Creating an Admin

8. Creating an OAuth 2.0 Client ID in GCP

To create an OAuth 2.0 client ID in GCP (Google Cloud Platform), follow these steps:

  1. Access the GCP administrator screen and select "Credentials" from the API menu on the side.
  2. On the next screen, select "CREATE CREDENTIALS" and choose "OAuth Client ID" from the drop-down menu.
  3. On the next screen, configure the following:

9. Entering OAuth 2.0 Client ID and Key in Django Admin

After successfully creating an OAuth client ID, you will receive an ID and secret key. Let's enter these into Django Admin.

Navigate to the Django admin page and select SITE from the left menu. Then, in the SOCIAL ACCOUNTS section, select SOCIAL APPLICATION. Let's look at each item in detail.

9-1. SITES configuration

10. Project-level `urls.py`

11. Creating the Template

11-1. Creating base.html and index.html

11-2. Creating a Page with the Google Login Button Displayed

11-3. Creating a profile page for each user type

11-4. template authentication_error.html and signup.html copied from allauth

12. Creating Views

13. Creating an Adapter for registration and login

14. Creating urls.py on the Application Side

15. Creating a Logout Function

16. Avoiding Duplicate Users for the Shop and Customer Models

However, even if the application appears to be working properly at this point, a user whose profile displays at accounts/shop/aaa can still access accounts/customer/aaa. That isn’t the desired behavior, so we need to ensure users cannot access pages for the other user type.

The repository for this authentication application we have implemented can be found here.

To address this issue more fully, I've written a Part 2 to this article. This tutorial is getting a bit long, so we'll end it here. Thanks for following along!