mastra-ai/mastra · error

Auth check failed (${res.status})

Error message

Auth check failed (${res.status})

What it means

fetchAuthState calls the auth status endpoint and treats 401/403 as 'auth enabled but not authenticated' and a missing response as 'auth disabled'. Any other non-ok status means the auth check itself malfunctioned, so it throws 'Auth check failed (<status>)' rather than guessing the auth state. Callers like useFactoryAuth receive this rejection.

Source

Thrown at mastracode/factory-ui/src/ui/domains/auth/services/auth.ts:127

  input: { name: string; email: string; password: string },
): Promise<void> {
  return postBetterAuthCredentials(baseUrl, 'sign-up/email', input);
}

/**
 * Fetch the current auth state from `/auth/me`. When the route is missing (auth
 * disabled), reports `authEnabled: false` so the UI hides all auth affordances.
 */
export async function fetchAuthState(baseUrl: string): Promise<FactoryAuthState> {
  const res = await fetch(`${baseUrl}/auth/me`, { headers: { Accept: 'application/json' }, credentials: 'include' });
  if (res.status === 404) {
    return { authEnabled: false, authenticated: false };
  }
  if (res.status === 401 || res.status === 403) {
    return { authEnabled: true, authenticated: false };
  }
  if (!res.ok) {
    throw new Error(`Auth check failed (${res.status})`);
  }
  const data = (await res.json()) as {
    authenticated?: boolean;
    user?: { userId?: string; email?: string; name?: string; organizationId?: string } | null;
    provider?: string;
    signUpDisabled?: boolean;
  };
  return {
    authEnabled: true,
    authenticated: Boolean(data.authenticated),
    user: data.user ?? undefined,
    provider: data.provider,
    signUpDisabled: data.signUpDisabled,
  };
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Look at the status code in the message: 404 means the auth route/baseUrl is wrong; 5xx means a server-side problem.
  2. Verify baseUrl targets the server that actually mounts the auth state endpoint.
  3. Check server logs for the underlying 5xx cause (DB, migration, crash) and fix/restart the backend.
  4. In the client, catch this error and show a 'cannot verify session' banner with a retry instead of treating the user as logged out.

Example fix

// before
const { data } = useQuery({ queryFn: fetchAuthState }); // unhandled throw breaks the tree

// after
const { data, error, refetch } = useQuery({ queryFn: fetchAuthState, retry: 2 });
if (error) return <AuthUnavailableBanner onRetry={refetch} />;
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight reachability before the auth check
const healthy = await fetch(`${baseUrl}/healthz`).then(r => r.ok).catch(() => false);
if (!healthy) throw new Error('Server unreachable');

Try / catch

try {
  const state = await fetchAuthState();
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Auth check failed')) {
    // parse status from message; show retry banner, do NOT log the user out
  }
}

Prevention

When it happens

Trigger: The auth status endpoint returns 5xx (server crash, DB down), 404 (route not mounted / wrong baseUrl), or unexpected 3xx/4xx that is not 401/403 — e.g. a reverse proxy answering 502 while the app backend is down.

Common situations: Deploying the frontend against a server without the auth routes (404); infrastructure outages behind a proxy returning 502/503; version mismatch where the auth endpoint moved; misconfigured baseUrl pointing at the wrong service.

Related errors


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