Unidy Docs

Authentication and session management

Restore, refresh and end sessions in the browser SDKs.

Let one SDK session manager own the customer's credentials. Use the HTTP token lifecycle when implementing your own transport or storage; this page describes the JavaScript and React libraries.

Storage and restoration

The browser SDKs keep the customer ID token in sessionStorage and the refresh token and sign-in context in localStorage. Closing a tab removes that tab's ID token but does not erase the persisted refresh credential. A subsequent tab may restore through refresh. Signing out clears stored SDK state.

The packages share storage keys on an origin. Treat them as one customer session, not independent accounts or tenants. In embedded or restricted browser environments, verify that storage and any required Unidy cookies are available.

InterfaceRestore and read state
Web componentsConfigure <u-config check-signed-in="true"> to attempt cookie-based restoration. Await Auth.getInstance() and auth.ready before a one-time state read. Use <u-signed-in> to gate customer UI.
ReactKeep <UnidyProvider> stable and use useSession(). Show a loading state while isLoading; use isAuthenticated after recovery finishes.
Core standaloneSupply deps.getIdToken; implement platform storage and restoration yourself.

An existing refresh credential and an existing Unidy browser cookie are separate restoration paths. Cookies may be unavailable across sites even when an SDK token flow works.

Obtain a valid token

javascript
import { Auth } from '@unidy.io/sdk/auth';

const auth = await Auth.getInstance();
await auth.ready;
const token = await auth.getToken();
if (typeof token !== 'string') {
  // Show an error/retry or sign-in prompt; token is an AuthError.
  return;
}
// Pass token only to an authenticated request that needs it; never log it.

In React, useSession().getToken() returns a current token or null, and service calls through the React client obtain it automatically. Read identity claims for presentation, but verify authentication independently at any server endpoint protecting your own application data.

Concurrent requests within a page share refresh work. The current libraries also account for a newer refresh token written by another tab and bound retries during rotation. A temporary network failure preserves recoverable credentials; a permanent invalid/revoked refresh credential requires sign-in. Keep provider autoRefresh enabled by default.

React to authentication

For web components, the sign-in root exposes authEvent and errorEvent; authentication event details include sensitive tokens, so do not log the whole event. Use authState / onAuthChange from @unidy.io/sdk/auth for state subscriptions and remove listeners when your view unmounts. In React, render from useSession and useLogin instead of building a second polling loop.

Sign out

Use <u-logout-button>, Auth.logout() or useSession().logout() rather than deleting one storage key. Clearing local state does not prove the server accepted revocation. In particular, React's convenience logout clears local state even if its sign-out request fails; applications that require confirmed server termination should inspect the underlying client.auth.signOut result before reporting that all access ended.

Web-component Auth.logout(true) requests global logout; the default varies with whether the session was recovered from a server session or started by the SDK. Ordinary SDK logout and global session termination have different scope. Neither promises termination of arbitrary third-party application sessions.

After logout, remove customer data from your own UI caches. On the next account switch, refetch profile, preferences, tickets and transactions for the new customer instead of reusing the previous customer's data.

Was this page helpful?