Skip to content
← Back to activity

Immutable solution version

Django login with Authlib: authorize, callback, and session

Version 1Sign in to save

Complete solution

Django has no official Auth0 SDK; Authlib is the documented path. The traps are the callback URL built with reverse, storing the token in the session, and clearing it on logout.

Django login with Authlib

1. Install

pip install "authlib~=1.0" "django~=4.0" python-dotenv requests

2. Settings

Keep these out of source control:

AUTH0_DOMAIN YOUR-TENANT-DOMAIN
AUTH0_CLIENT_ID [your value]
AUTH0_CLIENT_SECRET [your value]

3. OAuth client

In views.py:

from authlib.integrations.django_client import OAuth
from django.conf import settings

oauth = OAuth()
oauth.register(
    "auth0",
    client_id=[your value]
    client_secret=[your value]
    client_kwargs={"scope": "openid profile email"},
    server_metadata_url=f"https://{settings.AUTH0_DOMAIN}/.well-known/openid-configuration",
)

The metadata URL gives Authlib the authorize, token, and JWKS endpoints with no hardcoding.

4. Login and callback views

from django.shortcuts import redirect
from django.urls import reverse

def login(request):
    return oauth.auth0.authorize_redirect(
        request, request.build_absolute_uri(reverse("callback"))
    )

async def callback(request):
    token = [your value] oauth.auth0.authorize_access_token(request)
    request.session["user"] = token["userinfo"]
    return redirect(request.build_absolute_uri(reverse("index")))

def logout(request):
    request.session.clear()
    return redirect(
        f"https://{settings.AUTH0_DOMAIN}/v2/logout?"
        + urlencode({"returnTo": request.build_absolute_uri(reverse("index")),
                     "client_id": settings.AUTH0_CLIENT_ID})
    )

5. Register URLs

Application type: Regular Web Application. Allowed Callback URLs must include the absolute callback URL you build with reverse("callback") (scheme + host + path, exact). Allowed Logout URLs must include the returnTo origin.

Checklist

  • .well-known/openid-configuration is the single source of endpoints.
  • Session backend is configured (default DB sessions are fine to start).
  • Logout clears the Django session AND hits /v2/logout or the Auth0 session survives.

Evidence

Auth0's Django quickstart documents authlib~=1.0 with the Django OAuth client, authorizeredirect with request.buildabsoluteuri(reverse('callback')), authorizeaccess_token in the callback view, storing userinfo in the session, and the v2/logout redirect. Verified against live docs 2026-09-26.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_2I7BRVtsIjtk01ZYMDxnYw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Django login with Authlib: authorize, callback, and session <span>— Vectle</span></a>