mastra-ai/mastra · error

Missing authorization code

Error message

Missing authorization code

What it means

After all code-acquisition paths (browser callback, manual promise, prompt) the library still has no authorization code, so it cannot exchange anything for tokens and aborts the login. This terminal check guarantees loginOpenAICodex never proceeds to exchangeAuthorizationCode with an empty code.

Source

Thrown at mastracode/sdk/src/auth/providers/openai-codex.ts:686

      if (result?.code) {
        code = result.code;
      }
    }

    // Fallback to onPrompt if still no code
    if (!code) {
      const input = await options.onPrompt({
        message: 'Paste the authorization code (or full redirect URL):',
      });
      const parsed = parseAuthorizationInput(input);
      if (parsed.state && parsed.state !== state) {
        throw new Error('State mismatch');
      }
      code = parsed.code;
    }

    if (!code) {
      throw new Error('Missing authorization code');
    }

    const tokenResult = await exchangeAuthorizationCode(code, verifier, server.redirectUri);
    if (tokenResult.type !== 'success') {
      throw new Error('Token exchange failed');
    }

    const accountId = requireAccountId(tokenResult);

    return {
      access: tokenResult.access,
      refresh: tokenResult.refresh,
      expires: tokenResult.expires,
      accountId,
    };
  } finally {
    server.close();
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Re-run the login and actually paste the authorization code or full redirect URL when prompted.
  2. If headless, use the device-authorization flow (loginOpenAICodexDevice) instead of the browser callback flow.
  3. Verify the local callback port (e.g. localhost:1455) is not firewalled so the browser redirect can reach it.
  4. Implement options.onPrompt to reliably collect input; returning an empty string always leads to this error.

Example fix

// before: prompt returns nothing in headless env
await loginOpenAICodex({ onPrompt: async () => '' });

// after: use device login in headless environments
await loginOpenAICodexDevice({});
Defensive patterns

Strategy: try-catch

Validate before calling

function promptReturnsCode(onPrompt: (o: { message: string }) => Promise<string>): boolean {
  // ensure your onPrompt implementation rejects empty input before returning
  return typeof onPrompt === 'function';
}

Try / catch

try {
  await loginOpenAICodex({ onPrompt });
} catch (e) {
  if (e.message === 'Missing authorization code') {
    console.error('No code provided — falling back to device login.');
    await loginOpenAICodexDevice({});
  } else throw e;
}

Prevention

When it happens

Trigger: loginOpenAICodex completes callback wait, manual wait, and the onPrompt fallback with every path yielding an empty/absent code — e.g. onPrompt returns empty string, the browser never redirected to the local server, and no manual input was provided.

Common situations: User pressed Enter without pasting anything at the prompt; user abandoned the browser auth page; local callback server port blocked so the browser redirect never landed; headless/SSH environment where neither the browser nor the prompt is usable.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/ce429bd822d6148e. Report an issue: GitHub.