{"record":{"id":"0f555f5e099a1fe9","repo":"mastra-ai/mastra","slug":"auth-check-failed-res-status","errorCode":null,"errorMessage":"Auth check failed (${res.status})","messagePattern":"Auth check failed \\((.+?)\\)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory-ui/src/ui/domains/auth/services/auth.ts","lineNumber":127,"sourceCode":"  input: { name: string; email: string; password: string },\n): Promise<void> {\n  return postBetterAuthCredentials(baseUrl, 'sign-up/email', input);\n}\n\n/**\n * Fetch the current auth state from `/auth/me`. When the route is missing (auth\n * disabled), reports `authEnabled: false` so the UI hides all auth affordances.\n */\nexport async function fetchAuthState(baseUrl: string): Promise<FactoryAuthState> {\n  const res = await fetch(`${baseUrl}/auth/me`, { headers: { Accept: 'application/json' }, credentials: 'include' });\n  if (res.status === 404) {\n    return { authEnabled: false, authenticated: false };\n  }\n  if (res.status === 401 || res.status === 403) {\n    return { authEnabled: true, authenticated: false };\n  }\n  if (!res.ok) {\n    throw new Error(`Auth check failed (${res.status})`);\n  }\n  const data = (await res.json()) as {\n    authenticated?: boolean;\n    user?: { userId?: string; email?: string; name?: string; organizationId?: string } | null;\n    provider?: string;\n    signUpDisabled?: boolean;\n  };\n  return {\n    authEnabled: true,\n    authenticated: Boolean(data.authenticated),\n    user: data.user ?? undefined,\n    provider: data.provider,\n    signUpDisabled: data.signUpDisabled,\n  };\n}\n","sourceCodeStart":109,"sourceCodeEnd":143,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory-ui/src/ui/domains/auth/services/auth.ts#L109-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst { data } = useQuery({ queryFn: fetchAuthState }); // unhandled throw breaks the tree\n\n// after\nconst { data, error, refetch } = useQuery({ queryFn: fetchAuthState, retry: 2 });\nif (error) return <AuthUnavailableBanner onRetry={refetch} />;","handlingStrategy":"try-catch","validationCode":"// preflight reachability before the auth check\nconst healthy = await fetch(`${baseUrl}/healthz`).then(r => r.ok).catch(() => false);\nif (!healthy) throw new Error('Server unreachable');","typeGuard":null,"tryCatchPattern":"try {\n  const state = await fetchAuthState();\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Auth check failed')) {\n    // parse status from message; show retry banner, do NOT log the user out\n  }\n}","preventionTips":["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."],"tags":["auth","network","http","server-error"],"backgroundTag":"http-5xx-server-error","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}