Quickstart ¶
Prerequisites
Before starting, ensure you have:
Overview¶
Here's what we'll do:
graph LR
A[1. Add App] --> B[2. Configure Settings]
B --> C[3. Add URLs]
C --> D[4. Run Server]
D --> E[🎉 Working!]
style E fill:#4CAF50,color:white,stroke:none
Step 1: Register the App¶
Add django_gauth to your INSTALLED_APPS:
| settings.py | |
|---|---|
Session Middleware Required
Make sure django.contrib.sessions.middleware.SessionMiddleware is in your MIDDLEWARE.
Django Gauth uses sessions to store OAuth2 state and credentials.
Step 2: Add Settings¶
Add these variables at the bottom of your settings.py:
import os
# Google OAuth2 Credentials (from Google Cloud Console)
GOOGLE_CLIENT_ID = "your-client-id.apps.googleusercontent.com" # (1)!
GOOGLE_CLIENT_SECRET = "your-client-secret" # (2)!
# Where to redirect after successful login
GOOGLE_AUTH_FINAL_REDIRECT_URL = None # (3)!
# Session key names (safe defaults — customize if needed)
CREDENTIALS_SESSION_KEY_NAME = "credentials"
STATE_KEY_NAME = "oauth_state"
# OAuth2 scopes — what data you're requesting
SCOPE = [
"https://www.googleapis.com/auth/userinfo.email", # (4)!
"https://www.googleapis.com/auth/userinfo.profile",
"openid",
]
# ⚠️ ONLY for local development (allows HTTP instead of HTTPS)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # (5)!
- Get this from your Google Cloud Console → Credentials → OAuth 2.0 Client
- Keep this secret! Use environment variables in production.
- Set to
Noneto redirect back to the Gauth landing page (/gauth/) - Always include email + profile + openid for basic user info
- Remove this in production! HTTPS is mandatory for deployed apps.
Never commit secrets!
Use environment variables or .env files for your credentials:
Step 3: Include URLs¶
Add the Gauth URLs to your root urls.py:
| urls.py | |
|---|---|
This registers three endpoints:
| URL | Purpose |
|---|---|
/gauth/ |
Landing page with "Authenticate" button |
/gauth/login/ |
Initiates OAuth2 flow → redirects to Google |
/gauth/login-callback |
Handles Google's response after consent |
Debug Endpoint
When DEBUG=True, an additional endpoint is available:
| URL | Purpose |
|---|---|
/gauth/debug |
Shows sanitized session data as JSON |
Step 4: Run & Test¶
You're done!
Navigate to http://127.0.0.1:8000/gauth/ to see the landing page.
Click Authenticate → Sign in with Google → You're redirected back, now authenticated!
What Just Happened?¶
sequenceDiagram
participant U as 👤 User
participant D as 🖥️ Django App
participant G as 🔑 Google
U->>D: Visit /gauth/
D-->>U: Show landing page
U->>D: Click "Authenticate"
D->>G: Redirect to Google consent
G-->>U: Show account picker
U->>G: Select account & consent
G->>D: Redirect to /gauth/login-callback
D->>G: Exchange code for tokens
G-->>D: Return access + ID tokens
D->>D: Store credentials in session
D-->>U: Redirect to final page ✓
Next Steps¶
-
Configure further
-
Deploy to production
-
Understand the flow
-
Something broken?