A session store designed to preserve the session between several Node.js instances.
This class allow to handle a session inside a Redis DB. You can init a session, destroy it, get session, update sessions properties and check if user is authenticated.
The life-time of a given session is maximum 10 minutes by default.
interface Store {
/** Return callback URL as string **/
returnTo?: string;
}
interface FrameworkContext {
getCookie: (cookieName: string) => string;
setCookie: (cookieName: string, cookieValue: string | null, opts?: CookieSerializeOptions) => void
}
interface StoreContextOptions<T extends Store> extends TimedKVPeerOptions<T> {
/** Property name used in isUserAuthenticated() method to define if the user is authenticated or not **/
authenticationField?: keyof T;
/** HTTP Cookies options. Will be used when creating the session cookie. **/
setCookiesOptions?: CookieSerializeOptions;
}
import { StoreContext, MemoryAdapter } from "@myunisoft/redis";
const memoryAdapter = new MemoryAdapter();
const options = {
adapter: memoryAdapter,
authenticationField: keyof T | null;
cookiesOptions: SetOption;
}
const store: StoreContext = new StoreContext(options);
this method is used to initialize a session.
type InitSessionResponse = Result<string, "id must not be an empty string">;
const id = "foo";
const payload = {
returnTo = "theCallbackUrl";
}
const sessionResult = await store.initSession(id, ctx, payload);
if (sessionResult.ok) {
const id = sessionResult.unwrap(); // "foo"
}
this method is used to destroy the session.
await store.destroySession(ctx);
this method is used to get a session and attached data.
await store.getSession(ctx);
this method is used to update the properties of a session.
const payload = {
returnTo: "foo"
};
await store.updateSession(ctx, payload);
this method return a boolean to know if an user is authenticated.
await store.isUserAuthenticated(ctx);