Web Developer
I'm trying to create a function to verify the user's email on signup, the code below works fine, but it has a small problem, there has to be a session cookie in the browser or ask the user to log in for the verification to work.
I've been trying to create a session on the fly to use it for the verification, but it's not working I get the wrong token passed something....
Same when creating an Admin Client.
Is there a way to have the user just click the link to verify the email address, even if there's no stored login session in the browser?
"use server";
import { Client, Account } from "node-appwrite";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import Link from "next/link";
import { ID } from "node-appwrite";
import { isRedirectError } from "next/dist/client/components/redirect";
export async function createSessionClient() {
// Type assertion to cast environment variables to string
const endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT as string;
const project = process.env.NEXT_PUBLIC_APPWRITE_PROJECT as string;
// Runtime checks (optional but recommended)
if (!endpoint || !project) {
throw new Error("Missing required environment variables");
}
const client = new Client().setEndpoint(endpoint).setProject(project);
const session = cookies().get("appwrite-auth-cookie");
if (!session || !session.value) {
throw new Error("No session");
}
client.setSession(session.value);
return {
get account() {
return new Account(client);
},
};
}
export async function CreateSessionForVerify(userID: string, secret: string) {
"use server";
const { account } = await createSessionClient();
await account.updateVerification(userID, secret);
}