mongodb/node-mongodb-native · error · MongoOIDCError

Attempted to get a refresh token when none exists.

Error message

Attempted to get a refresh token when none exists.

What it means

Internal invariant of TokenCache (token_cache.ts:34): getRefreshToken() was called while refreshToken is undefined. The human workflow checks hasRefreshToken before calling getRefreshToken, so this throw should be unreachable through the public API. Reaching it implies a cache-state bug in the driver or direct use of the @internal TokenCache.

Solutions

  1. File a driver bug if reached via the public API with a reproduction
  2. When using internal APIs, check cache.hasRefreshToken before cache.getRefreshToken()
  3. Upgrade the driver in case the regression is already fixed

Example fix

// before
const rt = cache.getRefreshToken();

// after
const rt = cache.hasRefreshToken ? cache.getRefreshToken() : undefined;
Defensive patterns

Strategy: type-guard

Type guard

function getRefreshTokenSafe(cache: TokenCache): string | undefined {
  return cache.hasRefreshToken ? cache.getRefreshToken() : undefined;
}

Prevention

When it happens

Trigger: Calling the internal TokenCache.getRefreshToken() without the hasRefreshToken guard; or a regression where a workflow path calls getRefreshToken after removeRefreshToken cleared it.

Common situations: Misuse of internal APIs in a forked/custom auth provider. A driver regression in the human callback workflow's refresh-token branch.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/0255b3b73d7352a4. Report an issue: GitHub.

Appendix: source

Thrown at src/cmap/auth/mongodb_oidc/token_cache.ts:34

  get hasRefreshToken(): boolean {
    return !!this.refreshToken;
  }

  get hasIdpInfo(): boolean {
    return !!this.idpInfo;
  }

  getAccessToken(): string {
    if (!this.accessToken) {
      throw new MongoOIDCError('Attempted to get an access token when none exists.');
    }
    return this.accessToken;
  }

  getRefreshToken(): string {
    if (!this.refreshToken) {
      throw new MongoOIDCError('Attempted to get a refresh token when none exists.');
    }
    return this.refreshToken;
  }

  getIdpInfo(): IdPInfo {
    if (!this.idpInfo) {
      throw new MongoOIDCError('Attempted to get IDP information when none exists.');
    }
    return this.idpInfo;
  }

  put(response: OIDCResponse, idpInfo?: IdPInfo) {
    this.accessToken = response.accessToken;
    this.refreshToken = response.refreshToken;
    this.expiresInSeconds = response.expiresInSeconds;
    if (idpInfo) {
      this.idpInfo = idpInfo;
    }

View on GitHub (pinned to dce7939f86)