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.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?
Recommended threads
- Transaction Error
AppwriteException: Transaction with the requested ID could not be found. at Generator.next (<anonymous>) { code: 404, type: 'transaction_not_found', r...
- Can not get the logged in user data in N...
I'm trying to get the user data after i log in with otp but it get this error : AppwriteException: User (role: guests) missing scopes (["account"]) at Gen...
- Sites Runtimes 1.8.0 Self-Hosting. Flutt...
I have a problem with the Flutter version for SITES RUNTIMES. I use self-hosting and updated to version 1.8.0, which according to the changelog says they updat...