Skip to main content

Credential Storage

BrowserVault

BrowserVault persists connection credentials in browser cookies so they survive page reloads without requiring a fresh API call.

Usage

import { BrowserVault } from "@armaanjain/courier-web-sdk";

const vault = new BrowserVault("myapp_", 30); // prefix, TTL in days

Store Credentials

vault.store({
host: "broker.example.com",
port: 443,
clientId: "client-123",
username: "user",
password: "token",
scheme: "wss",
path: "/mqtt",
topic: "my/default/topic", // optional
});

Retrieve Credentials

const saved = vault.retrieve();
// Returns EndpointConfig | null

if (saved) {
const client = new RealtimeClient({ endpoint: saved });
} else {
// No saved credentials — fetch from API
}

Returns null if essential fields (host, clientId, username, password) are missing.

Clear Credentials

vault.purge();

How It Works

Each field is stored as a separate cookie with the configured prefix:

CookieContent
myapp_hhost
myapp_pport
myapp_cidclientId
myapp_uusername
myapp_pwpassword
myapp_scscheme
myapp_papath
myapp_cscleanSession
myapp_kakeepAliveSeconds
myapp_ttopic (optional)

Cookies are set with SameSite=Lax and the configured TTL (default 30 days).