Session Transports

Session transports are used by the sessions middleware to store and retrievesession identifiers in requests and responses. Session transports mustimplement the get and/or set methods and can optionally implement theclear method.

get

transport.get(request): string | null

Retrieves a session identifier from a request object.

If present this method will automatically be invoked for each transport untila transport returns a session identifier.

Arguments

  • request: Request

Request object to extract a session identifier from.

Returns the session identifier or null if the transport can not find asession identifier in the request.

Examples

  1. get(req) {
  2. return req.get('x-session-id') || null;
  3. }

set

transport.set(response, sid): void

Attaches a session identifier to a response object.

If present this method will automatically be invoked at the end of a requestregardless of whether the session was modified or not.

Arguments

  • response: Response

Response object to attach a session identifier to.

  • sid: string

Session identifier to attach to the response.

Returns nothing.

Examples

  1. set(res) {
  2. res.set('x-session-id', value);
  3. }

clear

transport.clear(response): void

Attaches a payload indicating that the session has been cleared to theresponse object. This can be used to clear a session cookie when the sessionhas been destroyed (e.g. during logout).

If present this method will automatically be invoked instead of set when thereq.session attribute was removed by the route handler.

Arguments

  • response: Response

Response object to remove the session identifier from.

Returns nothing.