#+CREATED: [2025-06-06 Fri 14:01]
#+DATE: [2025-06-06 Fri 14:01]
* django-auth0-auth

[[https://pypi.org/project/django-auth0-authbackend/][https://img.shields.io/pypi/v/django-auth0-authbackend.svg]]
[[https://github.com/andyreagan/django-auth0-auth/actions/workflows/python-test-publish.yml][https://github.com/andyreagan/django-auth0-auth/actions/workflows/python-test-publish.yml/badge.svg]]

Authentication backend for Django with Auth0.
As of June 2025, all of the libraries that I saw for Django Auth0 target old versions of Django,
we'll start this by supporting version 5+ and python 3.11+ (3.10 only has 1 year of life).
Also, some of these don't actually subclass Django's =AuthBackend= and implement a login system that is more
"beside" Django than integrated with it.
Because this is fully integrated, we can use Django's built-in =@login_required= decorator and it's auth Mixins.

This project is not affiliated with Auth0.

*Features:*
- Fully automated end-to-end testing with Playwright to ensure Auth0 integration works correctly
- Complete Django authentication backend integration using Django's built-in auth system
- Support for modern Django (5+) and Python (3.11+) versions

The names are slightly confusing because there are a lot of one-off projects 
(like this one)
on PyPI that attempt the same thing.
The repo name here is =django-auth0-auth=, 
the pypi name is =django-auth0-authbackend=,
and the importable package is =auth0=.
The installation instructions below reflect these names.

** Installation

Install the package from PyPI:

#+begin_src
pip install django-auth0-authbackend
#+end_src

** Usage

Take a look at the sample app provided in =sample/= to see how it's used in a MWE.
There are only a few steps.
First, include the app in your apps in your Django settings:

#+begin_src
INSTALLED_APPS = [
    ...,
    "auth0",
]
#+end_src

Next, include the auth settings and auth backend (also in your Django settings):

#+begin_src
AUTH0_CLIENT_ID = os.environ.get("AUTH0_CLIENT_ID")
AUTH0_CLIENT_SECRET = os.environ.get("AUTH0_CLIENT_SECRET")
AUTH0_DOMAIN = os.environ.get("AUTH0_DOMAIN")
AUTH0_AUDIENCE = os.environ.get("AUTH0_AUDIENCE")

AUTHENTICATION_BACKENDS = [
    "auth0.backend.Auth0Backend",
]
#+end_src

Finally, include the urls in your project =urls.py=:

#+begin_src
from django.urls import path, include

urlpatterns = [
    ...,
    path("auth0/", include("auth0.urls")),
]
#+end_src

** Running the sample app

First, create an auth0 application.

Set up python however you prefer, I'll use a virtual env:

#+begin_src
~/.pyenv/versions/3.11.10/bin/python -m venv .venv
source .venv/bin/activate
pip install .
#+end_src

Running the sample app, we can do:

#+begin_src
export AUTH0_CLIENT_ID=...
export AUTH0_CLIENT_SECRET=...
export AUTH0_DOMAIN=...
export AUTH0_AUDIENCE=...
python manage.py migrate
python manage.py runserver
#+end_src

Go to http://localhost:8000/auth0 and log in!

** Next steps

+ [X] Test that it works with the sample app (create an auth0 account to test)
+ [ ] Run through genAI to look for general improvements:
      is all the logic we have in the views/index the right place for this?
+ [X] Add pre-commit code checks
+ [X] Add automated release via github action
+ [X] Flesh out user documentation here in README
+ [X] Add automated tests (including full e2e testing with Playwright)
+ [ ] Profit
