Reference: TideCloak JavaScript SDK (@tidecloak/js)
A compact reference for the Vanilla JS SDK surface.
Install
npm install @tidecloak/js
Top-Level Exports
IAMService
High-level service for auth & E2EE. Also manages events.
Initialization
await IAMService.initIAM(config: AdapterJson & { redirectUri?: string }): Promise<void>IAMService.isLoggedIn(): boolean
Login/Logout
IAMService.doLogin(): voidIAMService.doLogout(): void
Tokens & Claims
await IAMService.getToken(): Promise<string> // Access token (JWT)IAMService.getIDToken(): string | undefined // ID token (JWT)IAMService.getTokenExp(): number | undefined // epoch secondsIAMService.getName(): string | undefined // preferred_usernameIAMService.getValueFromToken(key: string): anyIAMService.getValueFromIDToken(key: string): any
Roles
IAMService.hasRealmRole(role: string): booleanIAMService.hasClientRole(role: string, clientId?: string): boolean
Refresh
await IAMService.updateIAMToken(): Promise<boolean> // refresh if neededawait IAMService.forceUpdateToken(): Promise<boolean>
E2EE
type EncryptItem = { data: string | Uint8Array, tags: string[] };type DecryptItem = { encrypted: string, tags: string[] };await IAMService.doEncrypt(items: EncryptItem[]): Promise<string[]>await IAMService.doDecrypt(items: DecryptItem[]): Promise<(string|Uint8Array)[]>
Permissions:
_tide_<tag>.selfencryptto encrypt;_tide_<tag>.selfdecryptto decrypt. Output order matches input order.datamust bestringorUint8Array.
Events
type Handler = (...args: any[]) => void;IAMService.on(event: string, handler: Handler): typeof IAMServiceIAMService.off(event: string, handler: Handler): typeof IAMService
Events:
ready(authenticated: boolean)initError(error)authSuccessauthError(error)authRefreshSuccessauthRefreshError(error)logouttokenExpired
TideCloak
Lower-level adapter instance (Keycloak-style). Prefer IAMService unless you need advanced control.
Files You Must Provide
tidecloak.json(adapter from your realm client)public/silent-check-sso.html- A redirect page (default path
/auth/redirect)
Example: Minimal Bootstrap
<!-- index.html --><button id="login-btn">Log In</button><button id="logout-btn" style="display:none">Log Out</button><div id="status">Initializing...</div><script type="module" src="/main.js"></script>
// main.jsimport { IAMService } from "@tidecloak/js";import config from "./tidecloak.json";const login = document.getElementById("login-btn");const logout = document.getElementById("logout-btn");const status = document.getElementById("status");function ui(authed){ status.textContent = authed ? "✅ Authenticated" : "🔒 Please log in";login.style.display = authed ? "none" : "inline-block";logout.style.display = authed ? "inline-block" : "none";}login.onclick = () => IAMService.doLogin();logout.onclick = () => IAMService.doLogout();IAMService.on("ready", ui).on("tokenExpired", () => ui(false));await IAMService.initIAM(config);
Troubleshooting
- Renders as code block, not diagram: You're using a fenced ``` block. That's expected for code snippets; use Mermaid/Kroki only if configured in your docs site.
- Login succeeds but returns 404: Your redirect route/file doesn't exist. Create
/auth/redirect.htmlor set a customredirectUri. - Encrypt errors: Ensure
datais a string or Uint8Array, and the user has all required tag roles.