UNPKG

@nhost/nhost-js

Version:

Nhost JavaScript SDK

139 lines 4.54 kB
/** * Storage implementations for session persistence in different environments. * * This module provides different storage adapters for persisting authentication sessions * across page reloads and browser sessions. */ import type { StoredSession } from './session'; /** * Session storage interface for session persistence. * This interface can be implemented to provide custom storage solutions. * * **Important:** The methods here operate on {@link StoredSession}, which is * the enriched client-side session managed by the Nhost SDK. It extends the * raw `Session` type from `@nhost/nhost-js/auth` by adding a `decodedToken` * field with the parsed JWT payload. Do **not** use `auth.Session` here — * it is missing `decodedToken` and will cause a TypeScript error. * * @example * ```ts * import { type StoredSession, type SessionStorageBackend } from '@nhost/nhost-js/session'; * * class MyCustomStorage implements SessionStorageBackend { * get(): StoredSession | null { ... } * set(value: StoredSession): void { ... } * remove(): void { ... } * } * ``` */ export interface SessionStorageBackend { /** * Get the current session from storage * @returns The stored session or null if not found */ get(): StoredSession | null; /** * Set the session in storage * @param value - The session to store */ set(value: StoredSession): void; /** * Remove the session from storage */ remove(): void; } /** * Default storage key used for storing the Nhost session */ export declare const DEFAULT_SESSION_KEY = "nhostSession"; /** * Browser localStorage implementation of StorageInterface. * Persists the session across page reloads and browser restarts. */ export declare class LocalStorage implements SessionStorageBackend { private readonly storageKey; /** * Creates a new LocalStorage instance * @param options - Configuration options * @param options.storageKey - The key to use in localStorage (defaults to "nhostSession") */ constructor(options?: { storageKey?: string; }); /** * Gets the session from localStorage * @returns The stored session or null if not found */ get(): StoredSession | null; /** * Sets the session in localStorage * @param value - The session to store */ set(value: StoredSession): void; /** * Removes the session from localStorage */ remove(): void; } /** * In-memory storage implementation for non-browser environments or when * persistent storage is not available or desirable. */ export declare class MemoryStorage implements SessionStorageBackend { private session; /** * Gets the session from memory * @returns The stored session or null if not set */ get(): StoredSession | null; /** * Sets the session in memory * @param value - The session to store */ set(value: StoredSession): void; /** * Clears the session from memory */ remove(): void; } /** * Cookie-based storage implementation. * This storage uses web browser cookies to store the session so it's not * available in server-side environments. It is useful though for synchronizing * sessions between client and server environments. */ export declare class CookieStorage implements SessionStorageBackend { private readonly cookieName; private readonly expirationDays; private readonly secure; private readonly sameSite; /** * Creates a new CookieStorage instance * @param options - Configuration options * @param options.cookieName - Name of the cookie to use (defaults to "nhostSession") * @param options.expirationDays - Number of days until the cookie expires (defaults to 30) * @param options.secure - Whether to set the Secure flag on the cookie (defaults to true) * @param options.sameSite - SameSite policy for the cookie (defaults to "lax") */ constructor(options?: { cookieName?: string; expirationDays?: number; secure?: boolean; sameSite?: 'strict' | 'lax' | 'none'; }); /** * Gets the session from cookies * @returns The stored session or null if not found */ get(): StoredSession | null; /** * Sets the session in a cookie * @param value - The session to store */ set(value: StoredSession): void; /** * Removes the session cookie */ remove(): void; } //# sourceMappingURL=storageBackend.d.ts.map