Web Auth Models

File: api/nextango/apps/web_auth/models.py Last Updated: 2026-07-09

Overview

web_auth defines three pure-Django models (models.Model, no Sanity sync) that back session-based authentication for the dashboard and storefront: WebSession tracks active sessions, OTPCode tracks one-time passwords, and SessionPermission defines which endpoints each session type may reach. Field names are snake_case throughout.


WebSession

Tracks an active web authentication session for dashboard or demo access, for both authenticated users and guests.

Base class: models.Model

FieldTypeDescription
idUUIDField (primary key)UUID, generated on creation
session_keyCharField(40), uniqueLinks the row to Django's session framework
userForeignKey → User, nullable, CASCADEAuthenticated user; null for guest sessions. Related name web_sessions
session_typeCharField(20), choicesdemo or dashboard. Admin sessions are handled separately by AdminOTPAuthMiddleware
is_guestBooleanField, default FalseTrue for a demo session with no user account
emailEmailField, nullableGuest email, used for OTP verification when there is no user
cart_token_hashCharField(64), nullable, unique, indexedSHA-256 hash of a guest cart-session bearer token. A cookie-independent credential onto this session row, for guest cart operations in contexts where the session cookie does not round-trip
created_atDateTimeField, auto_now_addSession creation timestamp
expires_atDateTimeFieldExpiration timestamp. Defaults to 2 hours from creation for demo sessions and 7 days for dashboard sessions when not set explicitly
last_activityDateTimeField, auto_nowUpdated on every request that validates the session
ip_addressGenericIPAddressField, nullableClient IP captured at session creation
user_agentTextFieldBrowser user agent string

is_expired() returns True when expires_at has passed.

Serialized by WebSessionSerializer. Created, validated, and terminated by SessionService.


OTPCode

A one-time password code used for login, password recovery, email verification, and email change.

Base class: models.Model

FieldTypeDescription
idAutoField (primary key)Default Django integer primary key
emailEmailField, indexedRecipient email address, not unique
codeCharField(6)6-digit numeric code
created_atDateTimeField, auto_now_addCreation timestamp
expires_atDateTimeFieldExpiration timestamp. Defaults to 5 minutes from creation when not set explicitly
verifiedBooleanField, default FalseTrue once the code has been consumed
attemptsIntegerField, default 0Failed verification attempts; is_valid() returns False once this reaches 5
purposeCharField(20), choicesDeclared choices are login, password_recovery, verification. The views layer also creates OTPs with purpose='email_change', a value outside the declared choices

is_expired() returns True when expires_at has passed. is_valid() returns True only when the code is unverified, unexpired, and has fewer than 5 attempts.

Serialized by OTPCodeSerializer, which excludes code. Generated and verified by OTPService.


SessionPermission

A database-driven permission rule that controls which endpoints a session type (demo or dashboard) may reach.

Base class: models.Model

FieldTypeDescription
idAutoField (primary key)Default Django integer primary key
session_typeCharField(20), choicesdemo or dashboard
endpoint_patternCharField(255)Regex pattern matched against request.path
http_methodsJSONField, default listAllowed HTTP methods, for example ["GET", "POST"]. "*" allows any method
descriptionTextFieldHuman-readable explanation of what the rule allows
created_atDateTimeField, auto_now_addCreation timestamp
updated_atDateTimeField, auto_nowLast modification timestamp
is_activeBooleanField, default TrueInactive rules are not enforced

Serialized by SessionPermissionSerializer. Looked up by SessionService.get_permissions() and matched against the request path by WebSessionAuthMiddleware.


Was this page helpful?