lib/token
Interfaces
GetJoyTokenOptions
Options for getJoyToken.
Properties
force?
force?: boolean;Skip the cache and force a fresh exchange (use as the SDK onTokenExpired).
tokenRoute?
tokenRoute?: string;Override the token route (defaults to DEFAULT_TOKEN_ROUTE).
Variables
DEFAULT_TOKEN_ROUTE
const DEFAULT_TOKEN_ROUTE: "/api/joy-token" = '/api/joy-token';Default same-origin route the merchant mounts (export {loader} from '@joy-loyalty/weaverse-hydrogen/routes/joy-token').
GUEST_INFO_SKIP
const GUEST_INFO_SKIP: "joy:guest-info-skip" = 'joy:guest-info-skip';getInfo() (beat 2) rejects with an Error carrying this exact message when
the shopper is a GUEST. Beat 2 is a customer-fresh overlay; beat 1's metafield
blob already holds every shop-level field /headless/info would return, so a
guest needs no beat-2 call at all. The orchestrator (runBeatTwo) recognises
this message and keeps beat 1 silently — no /headless/info round-trip and no
redundant re-render — instead of surfacing it as an error.
Functions
clearJoyToken()
function clearJoyToken(): void;Drop any cached token (e.g. on logout).
Returns
void
dropJoyTokenIfCustomerMismatch()
function dropJoyTokenIfCustomerMismatch(customerId): boolean;Drop the cached token when it was not minted for customerId (the customer
SSR rendered this page for). A missing or different cached customerId counts
as a mismatch, so a legacy entry is refetched rather than trusted. Returns
whether the cache was dropped.
Parameters
| Parameter | Type |
|---|---|
customerId | string |
Returns
boolean
getCachedJoyCustomerFirstName()
function getCachedJoyCustomerFirstName(): string | null;The logged-in Shopify customer's first name that came back with the cached
token, or null (also once that token has expired). Synchronous peek, same
cache and expiry rule as hasCachedJoyToken;
read by the Loyalty Page host after a /headless/* call has resolved a token
(Liquid's greeting reads the theme's customer.first_name instead).
Returns
string | null
getJoyToken()
function getJoyToken(options?): Promise<string | null>;Resolve a usable customer JWT, or null for a guest. Serves a cached token
when one is still fresh; otherwise fetches (deduping concurrent callers).
Parameters
| Parameter | Type |
|---|---|
options? | GetJoyTokenOptions |
Returns
Promise<string | null>
hasCachedJoyToken()
function hasCachedJoyToken(): boolean;Synchronous peek at the cache getJoyToken maintains — no network
fetch, no async. For callers (the host adapter's AuthPort.getState())
that must answer "is there a live customer session?" without awaiting a
Promise. Reuses the same storage read + expiry check as getJoyToken
rather than a second parsing path.
Returns
boolean
revokeAndClearJoyToken()
function revokeAndClearJoyToken(options): void;Real-logout counterpart to clearJoyToken: tells the Joy Headless API
to revoke THIS device's token (POST {apiBase}/customer/auth/logout) before dropping
it locally. Fire-and-forget — the request is best-effort (keepalive so it
survives the navigation a logout usually triggers) and any failure (network,
non-2xx, no fetch) is swallowed; the local clear always happens
synchronously right after, exactly like a plain clearJoyToken() call.
Only call this from a genuine logout site. A guest / stale-SSR-cache guard
(nothing was ever "logged out" from the shopper's point of view) must keep
calling clearJoyToken() directly.
apiBase is the same Joy Headless API base URL other callers already
thread through (JoyConfig.apiUrl / HeadlessHostContext.apiUrl), e.g.
https://api.joy.so/app/api/v1/headless.
Parameters
| Parameter | Type |
|---|---|
options | { apiBase: string; } |
options.apiBase | string |
Returns
void