Skip to main content
Prefer AI-assisted setup? Use our AI prompts for Django to automatically integrate Civic Auth using Claude, ChatGPT, or other AI assistants. Includes a step-by-step video tutorial!
Important: The SDK Handles EverythingThe Civic 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

Or, if you’re using the uv package manager:

2. Configure Your Django Settings

Add Civic Auth configuration to your Django settings:

3. Add URL Patterns

Include the Civic Auth URLs in your project:

4. Create Basic Views

Create views for your application:

5. Add URL Patterns for Your Views

6. Run Your Django App

Visit http://localhost:8000 and click the login link to test authentication.

How It Works

Authentication Flow

  1. User visits /auth/login - starts the login process
  2. User authenticates with Civic
  3. User gets redirected to /auth/callback - completes authentication
  4. 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.civic_user as a dictionary:

Protecting Views

Use the @civic_auth_required decorator to protect views:

Template Usage

Access the user in Django templates:

Django REST Framework

For API views with Django REST Framework:

Complete Example

Here’s a complete working Django project structure:

Configuration Options

Note: redirect_url and post_logout_redirect_url must be absolute URLs.

Next Steps

  1. Get your Client ID: Sign up at auth.civic.com
  2. Replace YOUR_CLIENT_ID with your actual client ID
  3. Update URLs when deploying to production
  4. Add more protected views as needed
  5. Create templates for better user experience

Authentication Flows

Civic Auth supports multiple OAuth 2.0 authentication methods to provide maximum security for different application architectures.
Need client secret authentication? Civic 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 Civic Auth SDK and suitable for most applications.

Access Tokens

Read tokens from the session (or via a CivicAuth instance if exposed):