Skip to content

getAccessTokenSilently is typed Promise<string> but can resolve undefined #1186

Description

@catnipan

Checklist

Description

getAccessTokenSilently is typed as returning Promise<string> (src/auth0-context.tsx#L67), but at runtime it can resolve to undefined. TypeScript users therefore get no prompt to handle the undefined case, and the typical failure is a silent Authorization: Bearer undefined header sent to an API.

The undefined originates in auth0-spa-js's getTokenSilently, which auth0-react wraps and returns verbatim (the wrapper does token = await client.getTokenSilently(opts); … return token; with no guard). There are two runtime paths that produce undefined:

  1. cacheMode: 'cache-only' with a cache misssrc/Auth0Client.ts#L998:

    if (cacheMode === 'cache-only') {
      return;                      // bare return → undefined
    }
  2. Session ceiling reachedsrc/Auth0Client.ts#L979:

    if (await this._isSessionCeilingReached()) {
      return undefined;
    }

    Notably this fires on a plain getAccessTokenSilently() call with default options (when the cached ID token carries a session_expiry claim that has passed). It also clears the local session as a side effect and then resolves undefined rather than throwing — so callers get neither a token nor an exception.

The SDK's internals already acknowledge this: the private _getTokenSilently is typed Promise<undefined | GetTokenSilentlyVerboseResponse> and the public wrapper reads result?.access_token with optional chaining (src/Auth0Client.ts#L969) — the undefined is known, it's just erased at the public type boundary (src/Auth0Client.ts#L910, mirrored in auth0-react's Auth0ContextInterface).

Expected: the public return type should tell the truth — Promise<string | undefined> (or, if preferred, a narrowed overload so only cacheMode: 'cache-only' widens to string | undefined, with the session-ceiling path either included or changed to throw). Whatever shape is chosen in auth0-spa-js, auth0-react's re-declared getAccessTokenSilently type needs the same fix.

auth0-react version

2.22.1 (with @auth0/auth0-spa-js 2.24.1)

React version

19.2

Which browsers have you tested in?

Chrome

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions