Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Google Authentication in Django
Google Authentication allows users to log into your Django application using their Google accounts. This article demonstrates how to implement Google social authentication using Django Allauth library.
Setting Up Google OAuth Credentials
First, configure Google OAuth credentials in Google Cloud Console ?
Go to Google Cloud Console and create a project.
Navigate to OAuth consent screen and create a consent screen.
Go to Credentials and click "Create Credentials" ? "OAuth client ID".
-
Select "Web application" and add these URLs:
http://127.0.0.1:8000/http://127.0.0.1:8000/accounts/google/login/callback/
Save the generated Client ID and Client Secret for later use.
Django Project Setup
Installing Dependencies
Install the required package ?
pip install django-allauth
Configuring Settings
Add the following configuration to settings.py ?
SITE_ID = 1
LOGIN_REDIRECT_URL = "/"
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # Required for allauth
'allauth', # Core allauth
'allauth.account', # Account management
'allauth.socialaccount', # Social account support
'allauth.socialaccount.providers.google', # Google provider
'googleauthentication' # Your app name
]
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
URL Configuration
Configure the main urls.py file ?
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("accounts/", include("allauth.urls")), # Allauth URLs
path('', include("googleauthentication.urls")) # App URLs
]
Create app urls.py ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Creating Views and Templates
View Function
Create a simple view in views.py ?
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
HTML Template
Create templates/home.html with Google login functionality ?
<!DOCTYPE html>
<html>
<head>
<title>Google Authentication</title>
</head>
<body>
{% load socialaccount %}
<h1>Django Google Login</h1>
{% if user.is_authenticated %}
<p>Welcome, {{ user.first_name }}!</p>
<a href="/accounts/logout/">Logout</a>
{% else %}
<a href="{% provider_login_url 'google' %}?next=/">Login with Google</a>
{% endif %}
</body>
</html>
Database Setup and Admin Configuration
Run database migrations and create a superuser ?
python manage.py makemigrations python manage.py migrate python manage.py createsuperuser
Configuring Google Application in Admin
Start the development server:
python manage.py runserverGo to the admin panel at
http://127.0.0.1:8000/admin/Navigate to Sites and add a site with domain:
127.0.0.1:8000Go to Social applications and click "Add social application"
-
Configure the Google application:
- Provider: Google
- Name: Google
- Client ID: (from Google Cloud Console)
- Secret key: (from Google Cloud Console)
- Sites: Select your created site
Testing the Authentication
Visit http://127.0.0.1:8000/ and click "Login with Google". The application will redirect to Google's authentication page, and upon successful login, redirect back to your Django application with the user authenticated.
Key Features
| Feature | Description |
|---|---|
| OAuth 2.0 | Secure authentication protocol |
| User Profile | Access to email and profile information |
| Session Management | Automatic login/logout handling |
| Admin Integration | Easy configuration through Django admin |
Conclusion
Django Allauth simplifies Google authentication integration by providing pre-built views, templates, and OAuth handling. Configure the Google OAuth credentials, add the required settings, and create the social application in Django admin to enable seamless Google login functionality.
---