@foal/core
Version:
Full-featured Node.js framework, with no complexity
132 lines (131 loc) • 3.62 kB
TypeScript
import { SessionState } from './session-state.interface';
import { SessionStore } from './session-store';
/**
* Representation of a server/database/file session.
*
* @export
* @class Session
*/
export declare class Session {
private readonly store;
private readonly state;
private readonly oldFlash;
private oldId;
private status;
/**
* Retuns the user ID. If the session is anonymous, the value is null.
*
* @readonly
* @type {(string|number|null)}
* @memberof Session
*/
get userId(): string | number | null;
/**
* Returns true if the session has expired due to inactivity
* or because it reached the maximum lifetime allowed.
*
* @readonly
* @type {boolean}
* @memberof Session
*/
get isExpired(): boolean;
/**
* Returns the session expiration time in seconds.
*
* @readonly
* @type {number}
* @memberof Session
*/
get expirationTime(): number;
constructor(store: SessionStore, state: SessionState, options: {
exists: boolean;
});
/**
* Sets or replaces the userId associated with the session.
*
* @param {({ id: number|string })} user - The user containing the ID.
* @memberof Session
*/
setUser(user: {
id: number | string | object;
} | {
_id: number | string | object;
}): void;
/**
* Adds or replaces an element in the session. This operation is not saved
* in the saved unless you call the "commit" function.
*
* @param {string} key - The property key.
* @param {*} value - The property value.
* @param {{ flash?: boolean }} [options={}] If flash is true, the key/value
* will be erased at the end of the next request.
* @memberof Session
*/
set(key: string, value: any, options?: {
flash?: boolean;
}): void;
/**
* Gets the value of a key in the session content.
*
* @template T
* @param {string} key - The property key.
* @returns {(T | undefined)} The property value.
* @memberof Session
*/
get<T>(key: string): T | undefined;
get<T>(key: string, defaultValue: any): T;
/**
* Gets the session ID.
*
* @returns {string} - The session ID.
* @memberof Session
*/
getToken(): string;
/**
* Regenerates the session with a new ID. It is recommended
* to regenerate the session ID after any privilege level change
* within the associated user session.
*
* Common scenario: an anonymous user is authenticated.
*
* @returns {Promise<void>}
* @memberof Session
*/
regenerateID(): Promise<void>;
/**
* Destroys the session.
*
* @returns {Promise<void>}
* @memberof Session
*/
destroy(): Promise<void>;
/**
* Returns true if the method `destroy` has previously been called.
*
* @readonly
* @type {boolean}
* @memberof Session
*/
get isDestroyed(): boolean;
/**
* Saves or updates the session and extends its lifetime.
*
* If the session has already been destroyed, an error is thrown.
*
* This function calls periodically the store method "cleanUpExpiredSessions".
*
* @returns {Promise<void>}
* @memberof Session
*/
commit(): Promise<void>;
/**
* Returns the current time in seconds.
*
* @private
* @returns {number} The current time.
* @memberof Session
*/
private getTime;
private shouldCleanUpExpiredSessions;
private getTimeouts;
}