Back

Verify the email without a email/password session

  • 0
  • Auth
  • Web
  • Cloud
BABYLON
28 May, 2024, 23:13

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?

TypeScript
"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);
}
TL;DR
Issue with verify email without a session, resulting in 'general_unauthorized_scope' error. Change the approach by creating a new client without setting a session for the verify call. Edit the code as follows: - Remove the check for session.value. - When the cookie is set, everything works. - Use a different method to handle the session.
Steven
28 May, 2024, 23:58

you shouldn't need a session to call the account.updateVerification() 🧐

BABYLON
29 May, 2024, 00:19

That's why I'm losing my mind, I don't know why it's throwing that error.

I removed the following part and it's still saying something wrong with the token passed:

TypeScript
 if (!session || !session.value) {
    throw new Error("No session");
  }
TypeScript

"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) {
    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);
}
BABYLON
29 May, 2024, 00:21

When the cookie is set, everything is as expected. I'm using nextjs 14 btw.

Steven
29 May, 2024, 00:26

maybe it's still trying to set some cookie? Can you instantiate a new Client instead?

BABYLON
29 May, 2024, 00:35

Same story 😭

TypeScript

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);

  return {
    get account() {
      return new Account(client);
    },
  };
}
Steven
29 May, 2024, 01:17

You can't use the same code for the verify 🧐 you should leave this create session client as is for use where you need a session client. But for the verify call, you don't need a session client so instantiate a new client without setting the session

Steven
29 May, 2024, 01:19

And are you able to inspect the update verification API call?

BABYLON
29 May, 2024, 01:54

Finally, it's working now!! Thank you!! :appwritepeepo:

I created a new function to instantiate only a new client and it did it, even though I tried that before. : " )

Btw, the documentation here is using setSession so maybe it needs updating: https://appwrite.io/docs/references/cloud/server-nodejs/account#updateVerification

BABYLON
29 May, 2024, 01:54

or perhaps it for something else I don't know 🤷‍♂️

Steven
29 May, 2024, 02:07

Ya.. byproduct of auto generated docs 😬

<hadnet/>
28 Oct, 2024, 06:29

Sorry I'm struggling with this as well. I constantly got the error

TypeScript
{
  code: 401,
  type: 'general_unauthorized_scope',
  response: {
    message: 'User (role: guests) missing scope (account)',
    code: 401,
    type: 'general_unauthorized_scope',
    version: '1.6.0'
  }
}

When using session all it's working well, but the user needs to be logged in to verify its email. Can anyone help me with this? I'm using v1.6.0

<hadnet/>
28 Oct, 2024, 06:34

There is no frigging way I can use await account.createVerification or await account.updateVerification without a session. @BABYLON man, please, can you post your code to see how you solved, because I suspect that in the new version 1.6.0 it does not working.

Steven
28 Oct, 2024, 14:09

Create verification requires a session. Update does not. Double check what API call is throwing the error.

If you still need help, create a separate #🚑│support post instead of posting in someone else's old thread

<hadnet/>
28 Oct, 2024, 17:00

Thank you for the quick response. Unfortunately, account.updateVerification does not work without a session. I'm using node-appwrite, not the appwrite package, so it's possible that there is a bug, as I tried everything and it still didn't work.

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