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
- Look at the status code in the message: 404 means the auth route/baseUrl is wrong; 5xx means a server-side problem.
- Verify baseUrl targets the server that actually mounts the auth state endpoint.
- Check server logs for the underlying 5xx cause (DB, migration, crash) and fix/restart the backend.
- 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
- Confirm baseUrl points at the server deployment that mounts the auth routes (404 guard).
- Monitor backend health; 5xx here usually means the app server or its DB is down.
- Configure query retry with backoff for the auth-state query.
- Treat this error as 'unknown state', not 'unauthenticated', in UI logic.
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
- request failed with status ${response.status}: ${responseTex
- Token exchange failed: ${error}
- Failed to fetch user info from Clerk
- Google service account token request failed (${response.stat
- Failed to create account
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/0f555f5e099a1fe9.
Report an issue: GitHub.