Middleware & Session Management
The DatabaseSessionMiddleware is the core security component that executes on every request.
Middleware Logic
- Extracts and decodes the JWT session token from the cookie or Bearer header.
- Periodically fetches the full session record from the database to ensure it's still active.
- Performs Session Hijack Detection by comparing the request's device and region with the session record. If they don't match, the session is invalidated.
- Rotates a per-request token (
random_string) within the session to mitigate replay attacks. - Injects
user_idandsession_idintorequest.statefor dependencies.