mastra-ai/mastra · error

Authentication failed / server-provided message

Error message

Authentication failed / server-provided message

What it means

postBetterAuthCredential posts credential sign-in/sign-up requests and, when the response is not ok, throws an Error whose message is either the server-provided JSON message or a generic 'Authentication failed' default. It surfaces the backend's better-auth rejection reason (bad credentials, disabled sign-up, rate limits, etc.) to the caller.

Source

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

 * session cookie is set by the response; the caller navigates afterwards.
 * Throws with the server's message so the sign-in form can display it.
 */
async function postBetterAuthCredentials(baseUrl: string, path: string, body: Record<string, string>): Promise<void> {
  const res = await fetch(`${baseUrl}/auth/api/${path}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
    credentials: 'include',
    body: JSON.stringify(body),
  });
  if (!res.ok) {
    let message = 'Authentication failed';
    try {
      const data = (await res.json()) as { message?: string };
      if (data?.message) message = data.message;
    } catch {
      // Non-JSON error body — keep the generic message.
    }
    throw new Error(message);
  }
}

/**
 * Full-page navigation after a successful credential sign-in, so the app boots
 * with the fresh session cookie. Service-level (like `redirectToLogin`) because
 * jsdom's `window.location.assign` is unforgeable in tests.
 */
export function navigateAfterSignIn(returnTo: string): void {
  window.location.assign(returnTo);
}

/** Email/password sign-in against the self-hosted better-auth provider. */
export function signInWithPassword(baseUrl: string, input: { email: string; password: string }): Promise<void> {
  return postBetterAuthCredentials(baseUrl, 'sign-in/email', input);
}

/** Email/password sign-up against the self-hosted better-auth provider. */

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the email/password are correct; retry credentials carefully (check caps-lock, trailing spaces).
  2. Read the thrown message: a server-provided message usually explains the exact reason (e.g. sign-up disabled).
  3. If you get the generic 'Authentication failed', inspect the network tab for the real status/HTML body — likely a proxy or server error, not bad credentials.
  4. For sign-up errors, confirm with the admin that registration is enabled on the server.

Example fix

// before
try {
  await signInWithPassword({ email, password });
} catch (e) {
  console.log('login broken'); // generic handling hides the server reason
}

// after
try {
  await signInWithPassword({ email, password });
} catch (e) {
  showToast((e as Error).message); // surfaces 'Invalid email or password' etc.
}
Defensive patterns

Strategy: try-catch

Validate before calling

// client-side sanity check before the request
if (!email.includes('@') || password.length === 0) {
  setFieldError('Enter a valid email and password');
  return;
}

Try / catch

try {
  await signInWithPassword({ email, password });
} catch (e) {
  const msg = e instanceof Error ? e.message : 'Authentication failed';
  if (msg === 'Authentication failed') {
    // generic: inspect network response; likely server/proxy issue
  }
  setFormError(msg);
}

Prevention

When it happens

Trigger: Calling signInWithPassword or signUpWithPassword with wrong email/password (401), sign-up when registrations are disabled (signUpDisabled), expired/blocked accounts, or any non-2xx from the auth endpoint — including non-JSON error bodies which keep the generic message.

Common situations: User typos their password; admin disabled sign-ups so registration fails; server behind a proxy returns an HTML error page (502/503) so the generic message appears; auth endpoint version mismatch returns unexpected status codes.

Understand the failure class

Related errors


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