Django Allauth's Google Login Redirect and Page Design

Currently, on the login page, I have a button:

<div class="d-grid gap-2">
    <a href="{% provider_login_url 'google' %}" class="btn btn-danger">
        <i class="fab fa-google"></i> Sign in with Google
    </a>
</div>

This redirects to accounts/google/login/, and that page allows for redirection to Google authentication.

I have two problems:

  1. I don't know if these two steps are necessary and I don't see the value of having the extra step accounts/google/login/.
  2. I don't know how to replace the standard layout of the accounts/google/login/ page (in case it is really needed).

To answer your first question, I really don't think an extra step is necessary. On the other hand, though, it depends on the application, and there's really nothing wrong with leaving your current option.

Regarding doing it all on one page, it's possible and would require minimal code, something like this should work if everything is set up correctly:

{#Other code#}  
<form method="POST" action="{% url 'google_login' %}">  
  {% csrf_token %}  
  <button type="submit">GOOGLE</button>  
</form>  
{#Other code#}  

We can immediately open the Google login window using form action=“{% url ‘google_login’ %}”. If you have SOCIALACCOUNT_LOGIN_ON_GET=True in your settings, then it will be: form method=“GET”....

p.s. If this option doesn't work for you, let me know, I can suggest something else.

Answering your two questions.

  1. I don't know if these two steps are necessary and I don't see the value of having the extra step accounts/google/login/.

Answer: By default, that’s how Django Allauth handles authentication. It’s required for the authentication process to work properly. (You can inspect the network activity to see what happens during this step.) If you want to skip this step, you can try overriding Allauth’s default login URLs. To do this, navigate to where Django Allauth is installed and tweak the code. (I haven’t tried this before, so proceed with caution.)

2.I don't know how to replace the standard layout of the accounts/google/login/ page (in case it is really needed

Answer: Look for the login.html file in your Django Allauth package. It’s likely in the /account/providers/google/ directory. To customize it:

Create a folder structure like this in your project directory: templates/account/providers/google/ Copy the login.html file from Allauth’s installation directory into this new folder. Modify the copied file to match your design. Once you’ve placed the updated file in your project’s templates folder, Django will automatically use it instead of the default one.

You're encountering two issues:

Unnecessary redirect to accounts/google/login/ before going to the Google authentication page. Customizing the layout of the accounts/google/login/ page if it's necessary.

  1. Unnecessary Redirect to accounts/google/login/ The extra redirect step to accounts/google/login/ is part of how django-allauth handles social login flows. This intermediate page is used to initiate the OAuth authentication process with Google. However, if you'd prefer to skip that page and go directly to Google for authentication, you can configure django-allauth to redirect immediately to the authentication provider without showing the intermediate page.

Here’s how you can modify this behavior:

Option 1: Use the socialaccount URL directly You can link directly to the Google login URL instead of relying on accounts/google/login/. This can be done by using the provider’s login URL without the intermediate page.

<div class="d-grid gap-2">
    <a href="{% provider_login_url 'google' %}?next=/your-redirect-url/" class="btn btn-danger">
        <i class="fab fa-google"></i> Sign in with Google
    </a>
</div>

This will bypass the intermediate accounts/google/login/ page and go directly to the Google authentication page. Alternatively, if you want more control over the authentication process or additional features, you can call the specific login URL directly:

<a href="/accounts/social/login/?process=google" class="btn btn-danger">
    <i class="fab fa-google"></i> Sign in with Google
</a>

Make sure that the redirect URL (next=/your-redirect-url/) is correct for where you want the user to go after successful authentication.

  1. Customizing the Layout of the accounts/google/login/ Page If you still need the intermediate page (accounts/google/login/) but want to customize its layout, you can override the templates used by django-allauth.

Here’s how you can customize the accounts/google/login/ page:

Override the template for django-allauth:

The relevant template is likely account/social/login.html or account/social/ (depending on the version). You can copy this template to your project and modify it to match your desired layout.

Create a templates/account/social/login.html file (if it doesn’t already exist in your project). Modify the HTML structure to your needs. For example, you can change button styles, add custom text, or integrate other design elements. Template Overriding Example:

<!-- templates/account/social/login.html -->
<div class="container">
    <h2 class="text-center">Sign in with Google</h2>
    <a href="{% provider_login_url 'google' %}" class="btn btn-danger">
        <i class="fab fa-google"></i> Continue with Google
    </a>
</div>

Ensure the Template is Found:

Ensure your custom templates are correctly set up in your settings.py file. For example, make sure your TEMPLATES setting includes the appropriate paths:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

This will allow you to have a customized login.html for the Google login, so the page no longer looks like the default django-allauth layout.

Back to Top