This guide shows how to pick a storage for session data and how to plug in your own implementation.
LocalStorage, SessionStorage, or MemoryStorageThis guide assumes you've completed the JavaScript quickstart or the installation guide.
You should already have:
@monocloud/auth-web-js SDK installedMonoCloudWebJSClient initializedBy default the SDK stores the session in LocalStorage so it survives page reloads and new tabs. Three implementations ship with the SDK:
| Storage | Persistence | When to use it |
|---|---|---|
LocalStorage | Until the user signs-out | Default. Shared across tabs and persists across reloads |
SessionStorage | The tab is open | When the session should end when the tab is closed |
MemoryStorage | The page is loaded | Tests, ephemeral previews, or when running inside an iframe with no storage |
Pick the storage at construction time:
import {
MonoCloudWebJSClient,
SessionStorage,
} from "@monocloud/auth-web-js";
export const client = new MonoCloudWebJSClient({
tenantDomain: "https://<your-domain>",
clientId: "<your-client-id>",
storage: new SessionStorage(),
});
Provide your own storage by implementing the IStorage interface. Anything that can asynchronously read, write, and delete string keys works - cookies, IndexedDB wrappers, native bridges, and so on.
import {
type IStorage,
MonoCloudWebJSClient,
} from "@monocloud/auth-web-js";
class CookieStorage implements IStorage {
async getItem(key: string): Promise<string | null> {
const match = document.cookie
.split("; ")
.find((row) => row.startsWith(`${key}=`));
return match
? decodeURIComponent(match.slice(key.length + 1)) || null
: null;
}
async setItem(key: string, value: string): Promise<void> {
document.cookie = `${key}=${encodeURIComponent(value)}; path=/; secure; samesite=strict`;
}
async removeItem(key: string): Promise<void> {
document.cookie = `${key}=; path=/; max-age=0`;
}
}
export const client = new MonoCloudWebJSClient({
tenantDomain: "https://<your-domain>",
clientId: "<your-client-id>",
storage: new CookieStorage(),
});
Things to keep in mind:
async - return Promise<string | null> and Promise<void>If you run two MonoCloudWebJSClient instances against the same clientId on the same origin (for example, two micro-frontends), set sessionKey so the storage keys don't collide:
import { MonoCloudWebJSClient } from "@monocloud/auth-web-js";
export const adminClient = new MonoCloudWebJSClient({
tenantDomain: "https://<your-domain>",
clientId: "<your-client-id>",
sessionKey: "admin",
});
export const portalClient = new MonoCloudWebJSClient({
tenantDomain: "https://<your-domain>",
clientId: "<your-client-id>",
sessionKey: "portal",
});
Each client now reads and writes its own session bucket.