userfront-core icon indicating copy to clipboard operation
userfront-core copied to clipboard

Add support for custom error handling and redirect

Open tyrw opened this issue 3 years ago • 0 comments

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

tyrw avatar May 24 '22 20:05 tyrw