README.md
django-gsi
Minimal Google Sign-In for Django. One Google Client ID, works on any Django project.
Installation
pip install git+https://github.com/akarca/django-google-auth.git
Setup (3 steps)
1. Settings
INSTALLED_APPS = [
...
"gsi",
]
TEMPLATES = [{
"OPTIONS": {
"context_processors": [
...
"gsi.context_processors.google_client_id",
],
},
}]
GOOGLE_CLIENT_ID = "your-client-id.apps.googleusercontent.com"
2. URLs
from django.urls import include, path
urlpatterns = [
...
path("", include("gsi.urls")),
]
3. Template
{% load gsi_tags %}
<!-- In your login form -->
{% google_signin_button %}
<!-- In your register form -->
{% google_signin_button text="signup_with" %}
That's it. Users who sign in with Google are automatically logged in (existing account) or registered (new account).
How It Works
- User clicks the Google Sign-In button
- Google's script sets a short-lived
g_csrf_tokencookie and redirects to Google (redirect mode) - The user picks an account; Google POSTs the JWT
credentialandg_csrf_tokento/accounts/google/callback/ - Backend checks that the posted
g_csrf_tokenequals the cookie - Backend verifies the JWT using
google-authlibrary - If email matches existing user → login
- If new email → create user with
unusable_password,email_confirmed=True - Redirect to
LOGIN_REDIRECT_URL, or to the postednextwhen it is on this site
Settings
| Setting | Default | Description |
|---|---|---|
GOOGLE_CLIENT_ID |
"" |
Google OAuth Client ID (required) |
LOGIN_REDIRECT_URL |
"/" |
Where to redirect after login |
LOGIN_URL |
"/accounts/login/" |
Where to redirect on error |
GSI_CALLBACK |
None |
Post-login hook (see below) |
GSI_CSRF_CHECK |
True |
Check Google's g_csrf_token cookie against the posted value |
Post-Login Hook
For project-specific logic after Google login (cart migration, analytics, etc.):
# settings.py
GSI_CALLBACK = "myapp.auth.on_google_login"
# myapp/auth.py
def on_google_login(request, user, created):
"""Called after successful Google login/register."""
if created:
# New user — send welcome email, etc.
pass
# Migrate cart, set session data, etc.
User Model Compatibility
Works with any Django User model:
USERNAME_FIELD = "email"→ uses email as usernameUSERNAME_FIELD = "username"→ sets username to email- Has
full_namefield → sets from Google profile name - Has
first_name/last_name→ splits Google name - Has
email_confirmedfield → sets toTrue
Google Cloud Console Setup
- Go to Google Cloud Console
- Create a project (e.g. "Yuix Networks Inc")
- APIs & Services → Credentials → Create OAuth 2.0 Client ID
- Application type: Web application
- Authorized JavaScript origins: add all your domains
https://nameocean.nethttps://ipaddress.worldhttps://fenerbahce.fan
- Copy the Client ID to your Django settings
One Client ID works for all your sites.
License
MIT
Development
python runtests.py
Changelog
- 0.1.2: Google's
g_csrf_tokendouble-submit check (cookie set by Google's script,SameSite=None, 5 minutes, must equal the posted value). Without it a forged form could log the visitor into another Google account. A failed check redirects toLOGIN_URLwith "Google sign-in expired. Please try again." Emergency off switch:GSI_CSRF_CHECK = False. - 0.1.1: the
nextPOST parameter is followed only when it points to this site (url_has_allowed_host_and_scheme); other hosts fall back toLOGIN_REDIRECT_URL. Before, it was an open redirect.