Better-auth related question - localization.
Answered
Rhinelander posted this in #help-forum
RhinelanderOP
Hey, does anyone know if I can translate errors from better-auth?
Answered by Rhinelander
Found solutions - straight from the docs https://www.better-auth.com/docs/concepts/client#error-codes
const authClient = createAuthClient();
type ErrorTypes = Partial<
Record<
keyof typeof client.$ERROR_CODES,
{
en: string;
es: string;
}
>
>;
const errorCodes = {
USER_ALREADY_EXISTS: {
en: "user already registered",
es: "usuario ya registrada",
},
} satisfies ErrorTypes;
const getErrorMessage = (code: string, lang: "en" | "es") => {
if (code in errorCodes) {
return errorCodes[code as keyof typeof errorCodes][lang];
}
return "";
};
const { error } = await authClient.signUp.email({
email: "user@email.com",
password: "password",
name: "User",
});
if(error?.code){
alert(getErrorMessage(error.code), "en");
}
1 Reply
RhinelanderOP
Found solutions - straight from the docs https://www.better-auth.com/docs/concepts/client#error-codes
const authClient = createAuthClient();
type ErrorTypes = Partial<
Record<
keyof typeof client.$ERROR_CODES,
{
en: string;
es: string;
}
>
>;
const errorCodes = {
USER_ALREADY_EXISTS: {
en: "user already registered",
es: "usuario ya registrada",
},
} satisfies ErrorTypes;
const getErrorMessage = (code: string, lang: "en" | "es") => {
if (code in errorCodes) {
return errorCodes[code as keyof typeof errorCodes][lang];
}
return "";
};
const { error } = await authClient.signUp.email({
email: "user@email.com",
password: "password",
name: "User",
});
if(error?.code){
alert(getErrorMessage(error.code), "en");
}
Answer