Web Auth - Web Authentication & Session Management

Domain: Web Auth Location: api/nextango/apps/web_auth/ Mount: /auth/ (api/nextango/urls.py:155) Last Updated: 2026-07-09

Overview

The web_auth app provides session-based authentication for web clients: the dashboard and the storefront. It covers email-first login (password or OTP), customer registration, password recovery, session lifecycle, and profile editing. Sessions are Django session cookies backed by a WebSession database record, not JWT or bearer tokens.

PIN login for POS terminals lives in the users app (authenticate_pin_login, consumed by pos_gateway) and is out of scope for this domain.

FileDescription
models.mdWebSession, OTPCode, SessionPermission models
serializers.mdRead-only response serializers and registration input serializers
views.mdAll 14 authentication API endpoints
services.mdOTPService, RateLimitService, SessionService, RegistrationService
signals.mdWhy web_auth uses service methods instead of Django signals
authentication.mdWebSessionAuthentication DRF class
middleware.mdWebSessionAuthMiddleware and IPBlockingMiddleware
examples.mdcurl examples for each auth flow

Key Features

Authentication flows

  • Session-based authentication using Django session cookies, not JWT or bearer tokens
  • Email-first entry point that routes the client to a password or OTP path
  • OTP verification with 6-digit codes and a 5-minute expiration
  • Password recovery through a non-consuming validate step followed by a consuming reset step
  • Guest demo sessions that do not require a user account
  • Customer registration with OTP verification and automatic guest-cart merge

Security

  • Progressive OTP rate limiting: 30 seconds, then 5 minutes, then 1 hour
  • Progressive account lockout on failed attempts: 15 minutes, 1 hour, 2 hours
  • IP-level protection against request flooding and authentication failure spam
  • Dev-mode mock superuser for guest sessions, active only when DEBUG=True
  • Email delivery through Resend with Django SMTP as a fallback

IP protection rules (IPBlockingMiddleware)

  • More than 30 requests from one IP in 1 second blocks that IP for 1 hour
  • More than 5 failed authentications (401 responses) from one IP in 10 seconds blocks it for 10 minutes
  • More than 20 failed authentications from one IP in 1 minute also blocks it for 10 minutes

API Endpoints

All paths are mounted under /auth/ at the application root (no /api/ prefix).

EndpointMethodDescription
/auth/check-email/POSTDetermine whether the email should log in with a password or an OTP
/auth/login/POSTPassword login, OTP initiation, or dev-mode bypass
/auth/verify-otp/POSTVerify OTP and create a session, or prompt for password creation
/auth/set-password/POSTSet a password after OTP verification and create a session
/auth/register/initiate/POSTStart customer registration, sends OTP
/auth/register/verify/POSTComplete registration with OTP, creates User and Customer role
/auth/register/resend-otp/POSTResend the registration OTP
/auth/forgot-password/POSTStart password recovery, sends OTP
/auth/verify-recovery-otp/POSTValidate the recovery OTP without consuming it
/auth/reset-password/POSTConsume the recovery OTP and set a new password
/auth/session/GETCheck current session status and permissions
/auth/logout/POSTTerminate the session
/auth/update-email/initiate/POSTSend an OTP to a new email address for an email change
/auth/users/me/PATCHUpdate the current user's name or email

Full endpoint documentation: views.md

Architecture Highlights

Service layer, not signals

web_auth uses explicit service classes (OTPService, RateLimitService, SessionService, RegistrationService) instead of Django signals. Authentication steps such as rate-limit checks, OTP verification, and session creation have a strict order, and service methods make that order explicit in the view code rather than hidden in signal handlers.

Read more: signals.md

Middleware and DRF authentication work together

WebSessionAuthMiddleware validates the session once per request and attaches it to request.web_session. WebSessionAuthentication (a DRF authentication class) reuses that validation when present, or validates directly if the middleware did not run.

Read more: authentication.md, middleware.md

  • Users: User model, roles, and PIN login (authenticate_pin_login)
  • Web Commerce: Cart and checkout, resolves identity via WebSession
  • POS Gateway: Consumes PIN login from the users app, not web_auth

Key Architectural Decisions

  1. Session-based authentication over JWT, for simpler revocation on the web dashboard and storefront
  2. Service layer over signals, for explicit and testable authentication logic
  3. Progressive rate limiting and lockout, escalating with repeated failures
  4. Email-first flow that routes to password or OTP from a single entry point
  5. Cart-session continuity via cart_token_hash, a cookie-independent credential for guest carts

Next Steps

  1. models.md: data structures
  2. views.md: API endpoints
  3. authentication.md: DRF integration
  4. middleware.md: session and IP protection

Was this page helpful?