liveBook · Manning

Django authentication

As explained in Django in Action

Django authentication is the built-in Django mechanism that authenticates users through Django's user and session system.

Django authentication uses Django’s built-in user and session system to control access to API endpoints. It fits best when an API serves a web-based interface that can follow Django’s redirect to a login page. section 11.2.5

Apply it with Django Ninja by:

  • Importing django_auth from ninja.security. section 11.2.5
  • Passing django_auth to the auth argument on an endpoint or the NinjaAPI class. section 11.2.5
  • Choosing global, router-level, or endpoint-level protection. section 11.2.5

The important catch is the redirect: an unauthenticated API client must handle a web login form. That makes Django authentication suitable for browser-facing APIs, but awkward for APIs consumed directly by other systems. section 11.2.5

How Django in Action covers it

How deep it goes
Explains configuration scope, login redirects, and suitability, while stopping short of authentication internals.
Examples
Uses browser-facing APIs, single-page applications, and Vue.js clients to illustrate suitable authentication contexts.
What it assumes
Assumes Django user accounts and password management already exist, while explaining API-specific redirect consequences.
What you can do after
Apply it via Django Ninja by assigning django_auth globally, to a router, or to an individual endpoint.
How it connects
Connects Django authentication with Django Ninja, API authentication, user accounts, sessions, and API keys.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.