Skip to main content
zerotal

Documentation


Documentation / @zerotal/session / AuthSessionMiddleware

Class: AuthSessionMiddleware

Defined in: session/src/AuthSessionMiddleware.ts:26

Session middleware that also resolves the authenticated user.

Extends SessionMiddleware: after the session loads, it reads the numeric user_id from the session and calls the supplied findUser callback. When a user is returned it is attached to ctx.user, so Auth.user() works for the remainder of the request. If no user_id is stored (or it is not a number), the hook returns early and the request stays unauthenticated.

Example

app.use(class extends AuthSessionMiddleware {
  constructor() {
    super(new CookieDriver(env('SESSION_SECRET', 'changeme')), (id) => User.find(id));
  }
});

Extends

Constructors

Constructor

new AuthSessionMiddleware(driver, findUser): AuthSessionMiddleware

Defined in: session/src/AuthSessionMiddleware.ts:32

Parameters

driver

SessionDriver

Session driver used to load/save the session.

findUser

UserFinder

Callback resolving a user record by numeric ID (returns null when no matching user exists).

Returns

AuthSessionMiddleware

Overrides

SessionMiddleware.constructor

Properties

options

protected options: SessionOptions = {}

Defined in: session/src/SessionMiddleware.ts:43

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

Inherited from

SessionMiddleware.options

Methods

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

SessionMiddleware.with


afterResponse()?

optional afterResponse(ctx): Promise<void>

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

Parameters

ctx

HttpContext

Returns

Promise<void>

Inherited from

SessionMiddleware.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

SessionMiddleware.onError


withDriver()

static withDriver(driver): () => SessionMiddleware

Defined in: session/src/SessionMiddleware.ts:62

Build a zero-arg subclass with the given driver baked in, usable directly in app.use([...]) without an anonymous class wrapper.

Parameters

driver

SessionDriver

The session driver the returned middleware will use.

Returns

A constructable middleware class pre-bound to driver.

() => SessionMiddleware

Example

app.use([SessionMiddleware.withDriver(new CookieDriver(secret))]);

Inherited from

SessionMiddleware.withDriver