Back

missing scope (account)

  • 0
  • Web
DeFacedFace
25 Sep, 2024, 03:31

Registering a user works just fine in my application... Except adding user data to the collection simply does not work. I'm hit with the error :

TypeScript
Registration error: AppwriteException: app....
@service.cloud.appwrite.io (role: applications) missing scope (account)

my code:

TypeScript
import { getLoggedInUser, createUserClient } from "@/lib/server/appwrite";
import { redirect } from "next/navigation";
import { ID } from "appwrite";
import RegisterForm from "@/app/register/registerform";
import { cookies } from "next/headers";

export default async function RegisterPage() {
  const user = await getLoggedInUser();
  if (user) redirect("/dashboard");

  async function handleRegister(formData) {
    "use server";

    const { account } = await createUserClient();
    const {
      email,
      password,
      organization,
      missionStatement,
      organizationWebsite,
    } = formData;

    try {
      await account.create(ID.unique(), email, password, organization);
      const session = await account.createEmailPasswordSession(email, password);

      cookies().set("my-custom-session", session.secret, {
        path: "/",
        httpOnly: true,
        sameSite: "Strict",
        secure: true,
      });

      const data = {
        organization,
        missionStatement,
        organizationWebsite,
      };

      try {
        const request = await fetch("/app/api/register", {
          method: "POST",
          body: data,
        });
      } catch (error) {
        console.error(error);
      }

      // redirect("/dashboard");
    } catch (error) {
      console.error("Registration error:", error);
    }
  }

  return <RegisterForm onRegister={handleRegister} />;
}
TL;DR
Developers are receiving a "missing scope (account)" error when trying to add user data to a collection. The issue seems to be with using an API key incorrectly. The solution is to make sure the API key is set correctly in the createUserClient function.
DeFacedFace
25 Sep, 2024, 03:31

api route

TypeScript
import {
  Client,
  Databases,
  Account,
  ID,
  Permission,
  Role,
} from "node-appwrite";
import { cookies } from "next/headers";

export async function POST(request) {
  try {
    const cookie = cookies().get("my-custom-session");
    const client = new Client()
      .setEndpoint("https://cloud.appwrite.io/v1")
      .setProject("...")
      .setSession(cookie.value);

    const account = new Account(client);
    const databases = new Databases(client);

    try {
      const user = await account.get();
      const userID = user.$id;
      const data = await request.json();

      const result = await databases.createDocument(
        "...", // databaseId
        "...", // collectionId
        ID.unique(), // documentId
        data,
        [
          Permission.read(Role.user(userID)),
          Permission.write(Role.user(userID)),
        ],
      );

      console.log("Document created:", result);
      return result;
    } catch (err) {
      console.log("Error in document creation:", err);
    }
  } catch (error) {
    console.error("Error creating document:", error);
    throw error;
  }
}
DeFacedFace
25 Sep, 2024, 03:32

missing scope (account)

Steven
25 Sep, 2024, 03:47

you're using an API key somehwere where you shouldn't

DeFacedFace
25 Sep, 2024, 03:58

i'm using an api key here on the server sdk. What should i use instead?

TypeScript
export async function createUserClient() {
  const client = new Client()
    .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
    .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT)
    .setKey(process.env.NEXT_APPWRITE_KEY);

  return {
    client,
    account: new Account(client),
  };
}
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