userfront-core
userfront-core copied to clipboard
Add support for custom error handling and redirect
Right now, most of the methods have hard-coded error handling and redirects.
We should allow passing custom handleError and handleRedirect methods.
async function signupWithPassword({
username,
name,
email,
password,
userData,
redirect,
handleError,
handleRedirect
} = {}) {
try {
const { data } = await axios.post(`${store.baseUrl}auth/create`, {
tenantId: store.tenantId,
username,
name,
email,
password,
data: userData,
});
if (data.tokens) {
setCookiesAndTokens(data.tokens);
await exchange(data);
if (redirect === false) return data;
// Handle redirect
const redirectPath = redirect || getQueryAttr("redirect") || data.redirectTo || "/"
if (handleRedirect && typeof handleRedirect === "function") {
handleRedirect(redirectPath);
} else {
redirectToPath(redirectPath);
}
return data;
} else {
throw new Error("Please try again.");
}
} catch (error) {
// Handle error
if (handleError && typeof handleError === "function") {
handleError(error);
} else {
throwFormattedError(error);
}
}
}
Other
- [ ] Notify once deployed: https://app.chatwoot.com/app/accounts/3027/conversations/220