Skip to main content
zerotal

Documentation


Documentation / @zerotal/auth / PersistUserMiddleware

Class: PersistUserMiddleware

Defined in: auth/src/PersistUserMiddleware.ts:32

Reads 'user_id' from the active session and populates ctx.user.

This is the populate step, not a guard: it never blocks a request. If there is no user_id in the session the request simply continues as a guest (ctx.user = undefined). If a user_id is present but the user no longer exists, the stale session entry is cleared.

AuthProvider registers this globally, so ctx.user / Auth.user() are available everywhere — you do not wire it up yourself. To require an authenticated user on a route, use AuthMiddleware (the guard).

The user loader is resolved from the auth.userLoader container binding, which AuthProvider registers (from the convention default or AuthProvider.resolveUsing(...)).

A session whose second factor is still outstanding is not populated: the user is exposed only via Auth.pendingTwoFactorUser() so the challenge page can render, while ctx.user stays unset for every guard and handler. See TWO_FACTOR_PENDING_KEY.

After this middleware runs: Auth.check() → true if a user was found Auth.user() → the authenticated user (throws if guest) Auth.userOrNull() → the user or undefined

Extends

Constructors

Constructor

new PersistUserMiddleware(): PersistUserMiddleware

Defined in: core/src/middleware/BaseMiddleware.ts:36

Returns

PersistUserMiddleware

Inherited from

BaseMiddleware.constructor

Properties

options

protected options: object = {}

Defined in: auth/src/PersistUserMiddleware.ts:33

Subclasses must declare this with their default option values. TypeScript enforces this at compile time — forgetting it is a type error.

Overrides

BaseMiddleware.options

Methods

loadUser()

protected loadUser(userId, ctx): Promise<AuthUser | null>

Defined in: auth/src/PersistUserMiddleware.ts:44

Load the user for a given session user_id from the auth.userLoader container binding. Override this in a subclass for per-request loader logic.

Parameters

userId

number

The user_id read from the session.

ctx

HttpContext

The current HTTP context (used to reach the container).

Returns

Promise<AuthUser | null>

The user, or null when the id no longer resolves to a user.

Throws

when no auth.userLoader binding is registered (call AuthProvider.resolveUsing(...)).


handle()

handle(http, next): Promise<void | Response>

Defined in: auth/src/PersistUserMiddleware.ts:60

Parameters

http

HttpContext

next

NextFn

Returns

Promise<void | Response>

Overrides

BaseMiddleware.handle


with()

static with<T, Opts>(this, options): () => InstanceType<T>

Defined in: core/src/middleware/BaseMiddleware.ts:48

Returns a zero-arg subclass with the given options deep-merged on top of the subclass defaults, usable directly in app.use([...]).

Type Parameters

T

T extends (...args) => BaseMiddleware<any>

Opts

Opts = T extends (...args) => BaseMiddleware<U> ? U : object

Parameters

this

T

options

Partial<Opts>

Returns

() => InstanceType<T>

Inherited from

BaseMiddleware.with


afterResponse()?

optional afterResponse(ctx): Promise<void>

Defined in: core/src/middleware/BaseMiddleware.ts:70

Parameters

ctx

HttpContext

Returns

Promise<void>

Inherited from

BaseMiddleware.afterResponse


onError()?

optional onError(ctx, error): Promise<void>

Defined in: core/src/middleware/BaseMiddleware.ts:71

Parameters

ctx

HttpContext

error

Error

Returns

Promise<void>

Inherited from

BaseMiddleware.onError