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

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 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 following screen, enter the configuration items as shown below:
    • Select "Application type: Web application".
    • Choose any name for "Name".
    • For "Authorized Javascript origins", use http://127.0.0.1:8000 and also add http://localhost:8000 just in case. The default port number is 8000, but you can change it to another port number, such as 8019.
    • For "Authorized redirect URIs", use the following URLs: http://127.0.0.1:8000/accounts/google/login/callback/ and http://localhost:8000/accounts/google/login/callback/. Note that the trailing slash is required. Make sure to include it, or you may encounter errors.

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.

10. Creating urls.py for the project side

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, it is not. For instance, if a user whose profile is correctly displayed in accounts/shop/aaa, accesses accounts/customer/aaa, the profile will be displayed. This is not the expected behavior, so we need to ensure that users cannot access any other page.

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!