mastra-ai/mastra · error

Failed to create account

Error message

Failed to create account

What it means

If signUp() receives an OK (2xx) response but the parsed JSON body lacks result.user, the provider throws 'Failed to create account'. It is a defensive check for a success response that doesn't confirm a created user, indicating the server response shape doesn't match expectations.

Source

Thrown at auth/better-auth/src/index.ts:818

    const displayName = name ?? email.split('@')[0] ?? 'User';
    const headers = request?.headers ?? new Headers();

    // Use asResponse: true to get the full response with Set-Cookie headers
    const response = await this.auth.api.signUpEmail({
      body: { email, password, name: displayName },
      headers,
      asResponse: true,
    });

    if (!response.ok) {
      const errorData = (await response.json().catch(() => ({}))) as { message?: string };
      throw new Error(errorData.message || 'Failed to create account');
    }

    const result = (await response.json()) as { user?: User; token?: string | null };

    if (!result?.user) {
      throw new Error('Failed to create account');
    }

    // Extract Set-Cookie headers from Better Auth response
    const cookies: string[] = [];
    const setCookieHeader = response.headers.get('set-cookie');
    if (setCookieHeader) {
      // Split multiple cookies (they may be comma-separated or in multiple headers)
      cookies.push(...setCookieHeader.split(/,(?=\s*\w+=)/));
    }

    return {
      user: mapBetterAuthUserToEEUser(result.user),
      token: result.token ?? undefined,
      cookies,
    };
  }

  /**

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Align the installed better-auth version with the one this provider targets (check peer dependency range).
  2. Log the raw signUp response body to inspect the actual payload shape.
  3. Check whether email verification is enabled and handle the verification-required flow instead of expecting a user object.

Example fix

// before: email verification enabled -> 2xx with no user in body
await auth.signUp({ email, password, name });

// after: disable verification for local/test, or handle the verify flow
const ba = betterAuth({ emailAndPassword: { requireEmailVerification: false }, ... });
Defensive patterns

Strategy: type-guard

Validate before calling

if (res.ok) {
  const body = await res.json();
  if (!body?.user) console.warn('signUp returned 2xx without user; possible email-verification flow or version mismatch');
}

Type guard

function isSignUpResult(r: unknown): r is { user: User; token?: string | null } {
  return typeof r === 'object' && r !== null && 'user' in r && (r as { user: unknown }).user != null;
}

Try / catch

try {
  const result = await provider.signUp({ email, password, name });
  if (!isSignUpResult(result)) throw new Error('Sign-up succeeded but no user was returned');
} catch (e) {
  if (e instanceof Error && e.message === 'Failed to create account') {
    console.error('Check better-auth version and email-verification settings');
  }
  throw e;
}

Prevention

When it happens

Trigger: signUp({ email, password, name }) where response.ok === true but `result?.user` is falsy — empty body, better-auth version returning a different success payload, or middleware/proxy stripping the JSON body.

Common situations: better-auth version mismatch changing signUpEmail's return shape; a 200 returned after email-verification-required flows that don't include the user object; proxies or test harnesses returning empty 200s.

Related errors


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