URL Configuration ¶
Including Gauth URLs¶
Add to your root urls.py:
urls.py
from django.urls import path, include
urlpatterns = [
path('gauth/', include('django_gauth.urls')),
]
Custom prefix
You can change 'gauth/' to any prefix you want:
This would make your endpoints available at /auth/google/, /auth/google/login/, etc.
Available Endpoints¶
graph TD
subgraph "django_gauth URLs (app_name: django_gauth)"
A["/gauth/" — index]
B["/gauth/login/" — login]
C["/gauth/login-callback" — callback]
F["/gauth/logout/" — logout]
G["/gauth/session" — session]
D["/gauth/debug" — debug<br/><small>(DEBUG=True only)</small>]
end
A -->|"Authenticate button"| B
B -->|"Google redirects here"| C
C -->|"Redirect to final URL"| E[Your App]
E -->|"Poll auth state"| G
E -->|"Sign out"| F
style D fill:#FF9800,color:white
Endpoint Details¶
| Name | URL Pattern | View | Method | Purpose |
|---|---|---|---|---|
django_gauth:index |
/gauth/ |
index |
GET | Landing page |
django_gauth:login |
/gauth/login/ |
login |
GET | Start OAuth flow |
django_gauth:callback |
/gauth/login-callback |
callback |
GET | Handle OAuth response |
django_gauth:logout |
/gauth/logout/ |
logout |
GET | Clear session + revoke token |
django_gauth:session |
/gauth/session |
session_status |
GET | Auth-state probe (auto-refresh) |
django_gauth:debug |
/gauth/debug |
debug_information |
GET | Session debug info |
Using URL Names in Templates¶
Using URL Names in Views¶
from django.urls import reverse
login_url = reverse('django_gauth:login')
# Returns: '/gauth/login/'
Origin URL Support¶
The login endpoint supports an origin_url query parameter for post-auth redirects:
# Redirect user to login, then back to current page
login_url = f"/gauth/login/?origin_url={request.build_absolute_uri()}"
Same-origin only
The origin_url is validated to ensure it matches the current domain.
Cross-origin redirects are rejected for security.
flowchart TD
A[Login request with origin_url] --> B{Same origin?}
B -->|"scheme + netloc match"| C[✅ Use origin_url as redirect]
B -->|"Different origin"| D[❌ Use default redirect]
Callback URL for Google Console¶
The redirect URI you configure in Google Cloud Console must be:
Examples:
| Environment | Redirect URI |
|---|---|
| Local (127.0.0.1) | http://127.0.0.1:8000/gauth/login-callback |
| Local (localhost) | http://localhost:8000/gauth/login-callback |
| Production | https://yourdomain.com/gauth/login-callback |
No trailing slash!
The callback URL does not have a trailing slash. Make sure your Google Console redirect URI matches exactly.