Back

email verification

  • 0
  • Self Hosted
  • Auth
  • Web
discorrrdbomba
8 Apr, 2024, 23:49

I use something like this to create a signUp function:

TypeScript
      const verifyUrl = `${window.location.protocol}//${window.location.host}/${locale}/dashboard`;
      await account(locale).create(
        ID.unique(),
        email,
        password,
        name ? name : undefined
      );
      const session = await account(locale).createEmailPasswordSession(
        email,
        password
      );
      console.log(session);
      await account(locale).createVerification(verifyUrl);
      await account(locale).deleteSession(session.$id);
      setShowSuccess(true);
    } catch (err: any) {
      if (isAppwriteException(err)) {...```
...now the email get sent to the users email address successfully, including the redirect url.
A click to the redirect url opens the browser and the dashboard as intended. Inside of my nextjs 14 app's layout.js file I've got an AuthContext Provider wrapped around the app, which "onUseEffect" triggers the confirmVerification function (internal wrapper for updateVerification), which almost also works as intended - almost. The user gets verified (green tag in the console), but the updateVerification returns 401 stating "Invalid token passed in the request". See comment for the rest of my question -->
TL;DR
Developers are facing issues with the confirmVerification function while attempting to update verification and create a session. The issue may be related to timing/instances of the client/account being used. A possible solution could be to ensure the user is verified before triggering the function. The updateVerification function returns a 401 error stating "Invalid token passed in the request." This could be due to incorrect token handling or authentication issues. Double-check the token handling logic to troubleshoot the error.
discorrrdbomba
8 Apr, 2024, 23:49

Here the confirmVerification function (internal wrapper for updateVerification) from within AuthProvider:

TypeScript
  const userId = useSearchParams().get("userId") || "";
  const secret = useSearchParams().get("secret") || "";

  // ConfirmVerification
  const confirmVerification = async (): Promise<void> => {
    try {
      await account.updateVerification(userId, secret);
      const responseSession = await account.createSession(userId, secret);
      const responseAccount = await account.get();
      setSession(responseSession);
      setUser(responseAccount);

      toasty({
        variant: "success",
        header: (
          <>
            {tAuth("welcome")}{" "}
            <span
              style={{
                display: "inline-block",
                transform: "rotateY(180deg)",
                marginLeft: 2,
              }}>
              👋
            </span>
          </>
        ),
        msg: `${tAuth("signed_in_as")} ${"email"}`,
      });
    } catch (err: any) {
      if (isAppwriteException(err)) {
        toasty({
          variant: "error",
          buttonChildren: tAuth("dismiss_or_understood"),
          header: tAuth("auth_error"),
          msg: err.message,
        });
      } else {
        toasty({
          variant: "error",
          buttonChildren: tAuth("dismiss_or_understood"),
          header: tAuth("general_error"),
          msg: JSON.stringify(err),
        });
      }
    }
  };

  useEffect(() => {
    if (pathname === "/dashboard" && userId && secret) {
      confirmVerification();
    }
  }, []);```
Any Idea why this does not work out? Is it probably bcs of using wrong timing/instances of client/account?
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more