try {
// If the email is taken, this will throw an APIError error
const response = await auth.api.signUpEmail({
// I need this so that I can set the headers below
asResponse: true,
body: { name, email, password },
});
return redirect(redirectTo, {
// Setting the `Set-Cookie` header here, this will authenticate the user
headers: response.headers,
});
} catch (error) {
if (error instanceof APIError) {
// Here we have the APIError, with our error message
// See: https://www.better-auth.com/docs/concepts/api#error-handling
return data(
{
// This is for form management, ignore
result: submission.reply({
hideFields: ["password"],
formErrors: [error.body.message ?? error.message],
}),
},
{ status: 400 },
);
}
throw error;
}
try {
// If the email is taken, this will throw an APIError error
const response = await auth.api.signUpEmail({
// I need this so that I can set the headers below
asResponse: true,
body: { name, email, password },
});
return redirect(redirectTo, {
// Setting the `Set-Cookie` header here, this will authenticate the user
headers: response.headers,
});
} catch (error) {
if (error instanceof APIError) {
// Here we have the APIError, with our error message
// See: https://www.better-auth.com/docs/concepts/api#error-handling
return data(
{
// This is for form management, ignore
result: submission.reply({
hideFields: ["password"],
formErrors: [error.body.message ?? error.message],
}),
},
{ status: 400 },
);
}
throw error;
}