Django
Prefer AI-assisted setup? Use our AI prompts for Django to automatically integrate Leadovaro Auth using Claude, ChatGPT, or other AI assistants. Includes a step-by-step video tutorial!
Important: The SDK Handles Everything
The Leadovaro Auth Python SDK abstracts away all token validation complexity. You do NOT need to:
- Implement custom middleware for token validation
- Parse or validate JWT tokens manually
- Handle token refresh logic yourself
Simply use the provided decorators and user access functions - the SDK handles all authentication logic for you.
Quick Start
1. Install Dependencies
pip install "civic-auth[django]"
Or, if you're using the uv package manager:
uv add "civic-auth[django]"
2. Configure Your Django Settings
Add Leadovaro Auth configuration to your Django settings:
# settings.py
LEADOVARO_AUTH = {
"client_id": "YOUR_CLIENT_ID", # Get this from auth.leadovaro.com
"redirect_url": "http://localhost:8000/auth/callback",
"post_logout_redirect_url": "http://localhost:8000/"
}
# Add Leadovaro Auth middleware
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Add Leadovaro Auth middleware
'leadovaro_auth.integrations.django.LeadovaroAuthMiddleware',
]
3. Add URL Patterns
Include the Leadovaro Auth URLs in your project:
# urls.py
from django.contrib import admin
from django.urls import path, include
from leadovaro_auth.integrations.django import get_auth_urls
urlpatterns = [
path('admin/', admin.site.urls),
# Add Leadovaro Auth URLs
path('', include(get_auth_urls())),
# Your other URLs...
path('', include('your_app.urls')), # Replace with your app
]
4. Create Basic Views
Create views for your application:
# views.py
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from leadovaro_auth.integrations.django import leadovaro_auth_required
def home(request):
return HttpResponse("""
<h1>Welcome to My Django App!</h1>
<p><a href="/auth/login">Login with Leadovaro Auth</a></p>
<p><a href="/profile">View Profile</a> (requires authentication)</p>
<p><a href="/dashboard">Dashboard</a> (requires authentication)</p>
""")
@leadovaro_auth_required
def profile(request):
user = request.leadovaro_user
return JsonResponse({
"message": f"Hello, {user.get('name', 'User')}!",
"user_info": {
"id": user.get("id"),
"email": user.get("email"),
"name": user.get("name"),
"picture": user.get("picture")
}
})
@leadovaro_auth_required
def dashboard(request):
user = request.leadovaro_user
return HttpResponse(f"""
<h1>Dashboard</h1>
<p>Welcome, {user.get('name', 'User')}!</p>
<img src="{user.get('picture', '')}" alt="Profile picture" style="width: 50px; height: 50px; border-radius: 50%;">
<p>Email: {user.get('email', 'No email')}</p>
<p><a href="/auth/logout">Logout</a></p>
""")
def public_page(request):
return HttpResponse("<h1>Public Page</h1><p>No authentication required</p>")
5. Add URL Patterns for Your Views
# your_app/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('profile/', views.profile, name='profile'),
path('dashboard/', views.dashboard, name='dashboard'),
path('public/', views.public_page, name='public'),
]
6. Run Your Django App
python manage.py runserver
Visit http://localhost:8000 and click the login link to test authentication.
How It Works
Authentication Flow
- User visits
/auth/login- starts the login process - User authenticates with Leadovaro
- User gets redirected to
/auth/callback- completes authentication - User can now access protected views
Available Routes
/auth/login- Start authentication/auth/callback- Handle OAuth callback (auto-created)/auth/logout- Sign out user
Working with User Data
The authenticated user is available via request.leadovaro_user as a dictionary:
@leadovaro_auth_required
def user_info(request):
user = request.leadovaro_user
return JsonResponse({
"id": user.get("id"),
"email": user.get("email"),
"name": user.get("name"),
"picture": user.get("picture")
})
Protecting Views
Use the @leadovaro_auth_required decorator to protect views:
from leadovaro_auth.integrations.django import leadovaro_auth_required
@leadovaro_auth_required
def protected_view(request):
user = request.leadovaro_user
return HttpResponse(f"Hello {user.get('name', 'User')}, this is protected!")
Template Usage
Access the user in Django templates:
# views.py
from django.shortcuts import render
from leadovaro_auth.integrations.django import leadovaro_auth_required
@leadovaro_auth_required
def dashboard_template(request):
return render(request, 'dashboard.html', {
'leadovaro_user': request.leadovaro_user
})
<!-- templates/dashboard.html -->
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
{% if leadovaro_user %}
<h1>Welcome, {{ leadovaro_user.name|default:"User" }}!</h1>
<img src="{{ leadovaro_user.picture }}" alt="Profile picture">
<p>Email: {{ leadovaro_user.email|default:"No email" }}</p>
<a href="/auth/logout">Logout</a>
{% else %}
<p><a href="/auth/login">Please login</a></p>
{% endif %}
</body>
</html>
Django REST Framework
For API views with Django REST Framework:
from rest_framework.decorators import api_view
from rest_framework.response import Response
from leadovaro_auth.integrations.django import leadovaro_auth_required
@api_view(['GET'])
@leadovaro_auth_required
def api_profile(request):
user = request.leadovaro_user
return Response({
"authenticated": True,
"user": {
"id": user.get("id"),
"email": user.get("email"),
"name": user.get("name"),
"picture": user.get("picture")
}
})
@api_view(['GET'])
@leadovaro_auth_required
def api_dashboard(request):
user = request.leadovaro_user
return Response({
"message": f"Welcome to your dashboard, {user.get('name', 'User')}!",
"user_id": user.get("id")
})
Complete Example
Here's a complete working Django project structure:
# settings.py
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'your-secret-key'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your_app', # Replace with your app name
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'leadovaro_auth.integrations.django.LeadovaroAuthMiddleware',
]
ROOT_URLCONF = 'your_project.urls'
LEADOVARO_AUTH = {
"client_id": "YOUR_CLIENT_ID", # Replace with your actual client ID
"redirect_url": "http://localhost:8000/auth/callback",
"post_logout_redirect_url": "http://localhost:8000/"
}
# Database, static files, etc. (standard Django configuration)
# your_project/urls.py
from django.contrib import admin
from django.urls import path, include
from leadovaro_auth.integrations.django import get_auth_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(get_auth_urls())),
path('', include('your_app.urls')),
]
Configuration Options
| Field | Required | Description |
|---|---|---|
client_id | Yes | Your Leadovaro Auth Client ID from auth.leadovaro.com |
redirect_url | Yes | Where Leadovaro redirects after authentication (must be absolute URL) |
post_logout_redirect_url | Yes | Where users go after logout (must be absolute URL) |
Note: redirect_url and post_logout_redirect_url must be absolute URLs.
Next Steps
- Get your Client ID: Sign up at auth.leadovaro.com
- Replace
YOUR_CLIENT_IDwith your actual client ID - Update URLs when deploying to production
- Add more protected views as needed
- Create templates for better user experience
Authentication Flows
Leadovaro Auth supports multiple OAuth 2.0 authentication methods to provide maximum security for different application architectures.
Need client secret authentication? Leadovaro Auth supports PKCE-only, client secrets, and hybrid PKCE + client secret approaches. See our Authentication Flows guide for detailed comparison.
The examples above use PKCE authentication, which is handled entirely by the Leadovaro Auth SDK and suitable for most applications.
Access Tokens
Read tokens from the session (or via a LeadovaroAuth instance if exposed):
from django.http import JsonResponse
from leadovaro_auth.core import LeadovaroAuth # for key names
from leadovaro_auth.integrations.django import leadovaro_auth_required
@leadovaro_auth_required
def tokens(request):
return JsonResponse({
"access_token": request.session.get(LeadovaroAuth.ACCESS_TOKEN_KEY),
"id_token": request.session.get(LeadovaroAuth.ID_TOKEN_KEY),
"refresh_token": request.session.get(LeadovaroAuth.REFRESH_TOKEN_KEY),
"token_type": "Bearer",
})