mastra-ai/mastra · error · ProviderAuthRequiredError
Not logged in to xAI.
Error message
Not logged in to xAI.
What it means
buildXAIOAuthFetch wraps fetch to authenticate xAI (Grok) API calls with an OAuth bearer token. Before each request it reloads the credential store and requires an 'xai' credential of type 'oauth'. If none exists or the type differs, it throws ProviderAuthRequiredError('Not logged in to xAI.').
Source
Thrown at mastracode/sdk/src/providers/xai.ts:48
/** Set a custom AuthStorage instance (useful for tests / TUI integration). */
export function setAuthStorage(storage: AuthStorage | undefined): void {
authStorageInstance = storage ?? null;
}
/**
* Build a fetch wrapper that authenticates with xAI OAuth.
* Injects the access token (auto-refreshed by AuthStorage) as a bearer token,
* preserving non-auth headers from the caller.
*/
export function buildXAIOAuthFetch(opts: { authStorage?: CredentialStore } = {}): typeof fetch {
return (async (url: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {
const storage = opts.authStorage ?? getAuthStorage();
storage.reload();
const cred = storage.get(XAI_PROVIDER_ID);
if (!cred || cred.type !== 'oauth') {
throw new ProviderAuthRequiredError('Not logged in to xAI.');
}
// getApiKey() refreshes the access token if it has expired.
const accessToken = await storage.getApiKey(XAI_PROVIDER_ID);
if (!accessToken) {
throw new ProviderAuthRequiredError('Failed to refresh the xAI token.');
}
// Preserve existing headers, strip auth-related ones. Explicit init
// headers override headers carried by an incoming Request.
const headers = new Headers(url instanceof Request ? url.headers : undefined);
if (init?.headers) {
new Headers(init.headers).forEach((value, key) => headers.set(key, value));
}
headers.delete('authorization');
headers.delete('x-api-key');
headers.set('Authorization', `Bearer ${accessToken}`);View on GitHub (pinned to 75dd419e61)
Solutions
- Run the xAI OAuth login flow to store the 'xai' oauth credential.
- Verify the credentials store contains an 'xai' entry with type 'oauth'.
- If using API-key auth instead, use an api-key-based fetch/provider rather than buildXAIOAuthFetch.
- Check the opts.authStorage you pass actually contains the xai credential.
Example fix
// before
const fetchWithOAuth = buildXAIOAuthFetch(); // throws if never logged in
// after
if (!credentialStore.get('xai') || credentialStore.get('xai').type !== 'oauth') {
await xaiLogin(); // OAuth flow
}
const fetchWithOAuth = buildXAIOAuthFetch({ authStorage: credentialStore }); Defensive patterns
Strategy: validation
Validate before calling
const cred = storage.get('xai');
if (!cred || cred.type !== 'oauth') {
throw new Error('xAI login required; run the xAI OAuth flow first');
} Type guard
function isXAIOAuthCred(c: unknown): c is { type: 'oauth' } {
return !!c && typeof c === 'object' && (c as any).type === 'oauth';
} Try / catch
try {
return await fetchWithOAuth(url, init);
} catch (e) {
if (e instanceof ProviderAuthRequiredError && /Not logged in/.test(e.message)) {
await xaiLogin();
return await fetchWithOAuth(url, init);
}
throw e;
} Prevention
- Check xAI login state before constructing the provider.
- Keep one shared AuthStorage instance across login and request paths.
- Avoid mixing api-key and oauth credentials under the same provider id.
When it happens
Trigger: Any xAI model request routed through the OAuth fetch wrapper when the credential store has no 'xai' entry, or the entry's type is not 'oauth' (e.g. api-key credential or null after reload).
Common situations: User never completed xAI OAuth login; credentials cleared or logged out; an API-key credential stored under 'xai' so the type check fails; fresh/empty credential store in CI or tests; custom authStorage (opts.authStorage) that was never populated.
Related errors
- Not logged in to Kimi For Coding.
- Not logged in to OpenAI Codex.
- Failed to refresh the xAI token.
- State token has expired
- Redirect URI is required for SSO login
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/098376f8f13cf04a.
Report an issue: GitHub.