Skip to content

Engine: Auth

Customer sign-in state: AuthProvider and useAuth. The customer token itself lives in an httpOnly cookie that page scripts cannot read.

Generated from packages/engine/src/auth/: 1 files, 7 exported symbols. Signatures and types are shown as written in the source.

@magenxcommerce/engine/auth/auth-context

packages/engine/src/auth/auth-context.tsxclient

CUSTOMER_AUTH_FLAG_KEY const

const CUSTOMER_AUTH_FLAG_KEY = "magenx_auth"

Name of the non-secret, NON-httpOnly companion cookie marking that a customer session exists (value "1"). The real Magento customer token is NOT readable by JS — it lives in the httpOnly magenx_ct cookie, and the /api/graphql proxy injects it as the Authorization header on requests to Magento.

The companion cookie is set/cleared EXCLUSIVELY by the same server responses that set/clear magenx_ct (the session route + the social-login bridge), with the same maxAge — so the UI auth signal expires and disappears in lockstep with the actual session and cannot drift the way the old free-floating localStorage flag did (stale flag after an unclean logout / token expiry left the whole UI acting logged-in). A hand-forged cookie still flips the UI, but self-heals: every authed surface clears the session on the first structured auth error (see isAuthErrorFromError).

This constant doubles as the LEGACY localStorage key of the same name, which AuthProvider deletes on mount (one-time migration). Never write it to localStorage again, and never read auth state from document.cookie directly in feature code — go through useAuth().

hasAuthCookie function

hasAuthCookie(cookieString: string): boolean

Pure check: does the given Cookie string (i.e. document.cookie) contain magenx_auth=1? Exact name match — xmagenx_auth=1 or magenx_auth=0 do not count. Exported for tests.

isAuthError function

isAuthError(message: string): boolean

Returns true when the error message indicates an auth/token failure. English-substring matching — locale/version fragile, so prefer isAuthErrorFromError wherever the original error OBJECT is available; this string form remains for callers that only hold a message.

isAuthErrorFromError function

isAuthErrorFromError(err: unknown): boolean

Structured auth-failure detection: checks Magento's extensions.category on Apollo CombinedGraphQLErrors, falling back to the substring match on the error message. Use this over isAuthError when the error object is in hand (e.g. a useQuery error or a caught mutation error).

AuthContextValue interface

MemberTypeDescription
isAuthenticatedbooleanWhether a customer session exists. Derived from the non-secret magenx_auth companion cookie, which shares the token cookie's lifetime.
isInitializedbooleanTrue once the auth cookie has been read on mount.
signIn(token: string) => Promise<void>Completes a password sign-in: persists the freshly issued Magento token in the httpOnly cookie (awaited — the cookie must be set before any authenticated GraphQL call), then marks the session active. Cart-side effects (guest-cart merge) belong to the cart context, which wraps this in its own signIn.
markSignedIn() => voidMarks an externally-brokered sign-in (e.g. social login) as active. The OAuth bridge route has ALREADY minted the Magento token and set the httpOnly cookie server-side, so there is no token to persist here.
signOut() => voidClears the customer session: httpOnly cookie (best-effort) + auth flag.

AuthProvider function

AuthProvider({ children }: { children: ReactNode })

useAuth function

useAuth(): AuthContextValue