Cannot enable two-factor Authentication

Running auth.api.enableTwoFactor gives me a 401 error. How can I resolve this?

Versions

better-auth: 1.2.2
react-router: 7.2.0

To Reproduce


export async function action({ request }: Route.ActionArgs) {
  // ...validate
  try {
    const { user } = await auth.api.signInEmail({
      body: {
        email: submission.value.email,
        password: submission.value.password,
        rememberMe: true,
        asResponse: true,
      },
    });
    await auth.api.enableTwoFactor({
      body: { password: submission.value.password },
    }); // Error in this
    const { status } = await auth.api.sendTwoFactorOTP({
      body: {
        trustDevice: true,
      },
    });
    if (!user.emailVerified) return redirect('/auth/verify-email');
    return redirect('/auth/2fa');
  } catch (err) {
    // error
  }
}


Result


[APIError] {
  status: 'UNAUTHORIZED',
  body: undefined,
  headers: {},
  statusCode: 401
}
Was this page helpful?