Options
All
  • Public
  • Public/Protected
  • All
Menu

Class UserSessionManager<Device, Location>

Stateful implementation of the user sessions.
Session data is stored in external storage and client receives only it's id.
Sessions are implemented in a such way, so that they can be used in cluster or single-node infrastructures.

Type parameters

  • Device: DeviceBase

    Type of the device.

  • Location

    Type of the location.

Hierarchy

  • UserSessionManager

Constructors

constructor

  • Type parameters

    • Device: DeviceBase

    • Location

    Parameters

    • options: UserSessionManagerOptions<Device, Location>

      Options object.
      It should not be modified after, as it will be used without being cloned.

    Returns UserSessionManager<Device, Location>

Methods

create

  • create(subject: string, context: UserSessionOperationContext<Device, Location>, sessionTtl?: number): Promise<string>
  • Create new user session for subject.

    Parameters

    • subject: string

      Subject the session will belong to.

    • context: UserSessionOperationContext<Device, Location>

      Operation context.

    • Optional sessionTtl: number

      Session ttl. Takes precedence over the default one.

    Returns Promise<string>

    User session id.

delete

  • delete(subject: string, sessionId: string): Promise<void>
  • Delete user session.

    Parameters

    • subject: string

      Subject.

    • sessionId: string

      Id of the user session.

    Returns Promise<void>

deleteAll

  • deleteAll(subject: string): Promise<number>
  • Delete all of the user sessions.

    Parameters

    • subject: string

      Subject.

    Returns Promise<number>

    Number of deleted sessions.

read

  • read(subject: string, sessionId: string, context: UserSessionOperationContext<Device, Location>): Promise<[Readonly<UserSessionMetaData<Device, Location>>, null | string]>
  • Read user session from external storage.
    When idle functionality is activated, this method might delete user session and throw an error to notify about this, if it was idle for more than {@link UserSessionManagerOptions.timeouts.idle} seconds.
    When renew functionality is activated, this method might create a new user session, and return it's id as the second part of the tuple. In case renewed session id is returned, it needs to be sent to application clients via 'Set-Cookie' header to replace the old session cookie. The old session will still be available for {@link UserSessionManagerOptions.timeouts.oldSessionAvailabilityAfterRenewal} seconds, so that older requests might complete successfully and client has time to refresh session id on it's side.

    throws

    {Exception} With the following error codes: - ErrorCodes.USER_SESSION_NOT_FOUND - session wasn't found in the storage - ErrorCodes.USER_SESSION_EXPIRED - session is accessed from a device which differs from the one it was created - ErrorCodes.USER_SESSION_EXPIRED - session was expired because of the idle timeout

    Parameters

    • subject: string

      Subject.

    • sessionId: string

      Session id.

    • context: UserSessionOperationContext<Device, Location>

      Operation context.

    Returns Promise<[Readonly<UserSessionMetaData<Device, Location>>, null | string]>

    A tuple with the following parts: - session metadata - renewed session id (if renewal took place)

readAll

  • readAll(subject: string): Promise<ReadonlyMap<string, Readonly<UserSessionMetaData<Device, Location>>>>
  • Read all active sessions of the subject.

    Parameters

    • subject: string

      Subject.

    Returns Promise<ReadonlyMap<string, Readonly<UserSessionMetaData<Device, Location>>>>

    Active sessions of the subject.

renew

  • renew(subject: string, sessionId: string, sessionMetaData: UserSessionMetaData<Device, Location>, context: UserSessionOperationContext<Device, Location>): Promise<null | string>
  • Renew user session.
    Renewing consist from the following actions: 1. scheduling deletion of the old session in a very short amount of time 2. creating a new user session

    Parameters

    • subject: string

      Subject.

    • sessionId: string

      Id of the session to be renewed.

    • sessionMetaData: UserSessionMetaData<Device, Location>

      Metadata of that session.

    • context: UserSessionOperationContext<Device, Location>

      Operation context.

    Returns Promise<null | string>

    The new user session id.
    When renew can't be performed, a log message is printed and null is returned.

Static currentTimestamp

  • currentTimestamp(): number
  • Returns number

Static hash

  • hash(sessionId: string): string
  • Hashes session id.
    Useful for logging purposes.

    Parameters

    • sessionId: string

      Session id.

    Returns string

    Hashed session id.