#[SOLVED] AppwriteException: User (role: guests) missing scope (account)

54 messages · Page 1 of 1 (latest)

mint fable
#

Am I getting this error because of how I set up my settings?

#

From my Users collections

median wasp
#

What's your code?

mint fable
#

It's quite a bit of code. I want to be mindful that I'm not overwhelming ppl trying to help me. Hold on while I try to specific this...

#

Also, I'm not sure if this is a code issue.

median wasp
#

Seems you're getting an account, so seems it's an issue with your code 🤔

median wasp
mint fable
#

Hmm okay, so I think I identified the problem. And it has to do with appwrite permissions.

#

So in my database, Users collection, I set permissions to Any: all CRUD.

#

When I try to create an account for the first time, it gives the error:

#

But if I change the permissions in appwrite to All Users: all CRUD

#

it works

#

What I'm referring to:

#

I assumed Any would allow permissions for everyone, no matter what.

For some reason, it doesn't work that way for me. Maybe it's my code.

I tested All users, and that option successfully posted my account to the database and auth.

#

maybe im not understanding how appwrite works 😕

#

The previous error has to do with a TypeError, which I already opened a post for. So I didn't want to include it here, but here it is.

median wasp
# mint fable

But seems you're getting an account, not a database document/s

mint fable
median wasp
mint fable
#

Client Code:

// 2. Define a submit handler.
  async function onSubmit(values: z.infer<typeof SignupValidation>) {
    // create the user => async action that takes time
    const newUser = await createUserAccount(values);
    // if fails
    if (!newUser) {
      return toast({
        title: "Sign Up Failed. Please Try Again.",
      });
    }

    // create session for user
    const session = await signInAccount({
      email: values.email,
      password: values.password,
    }); 
    
    // check session
    if (!session) {
      return toast({ title: 'Sign in failed. Please try again.'})
    }
    
    // store session in REACT CONTEXT
    const isLoggedIn = await checkAuthUser();
    // if successful
    if (isLoggedIn) {
      // reset form
      form.reset();
      // return to homepage
      navigate('/')
    } else {
      return toast ({ title: "Sign Up Failed. Please Try Again."})
    }
  }
median wasp
#

Are you using an API key there?

mint fable
#

Hmm. I don't believe I'm using an API key

#

Can you be more specific on there?

median wasp
#

I think you're using server SDK instead of client SDK, but anyways, where did CreateUserAccount came from?

mint fable
#

Forgive me, I'm still new to coding so I don't know the difference between server and client SDK.

I set up my CreateUserAccounton the server side, which is then used by react-query as a mutation fx. On the client side, onSubmit of my form, triggers the react-query mutation fx to create a user and save to DB (which is the snippet I shared above)

#

SIGN UP

export async function createUserAccount(user: INewUser) {
  try {
    // create a new Auth User
    const newAccount = await account.create(
      ID.unique(),
      user.email,
      user.password,
      user.name
    );
    
    // if no new Account
    if (!newAccount) throw Error;
    
    const avatarUrl = avatars.getInitials(user.name);
    
    // create new user in DB
    const newUser = await saveUserToDB({ 
      accountId: newAccount.$id,
      name: newAccount.name,
      email: newAccount.email,
      username: user.username,
      imageUrl: avatarUrl,
    });
    
    return newUser;
  } catch (error) {
    console.log(error);
    return error;
  }
}
median wasp
#

You have a custom node server to create the account?

#

If so, it's not needed, Appwrite is the server

#

You should create the account, login, etc client sided

mint fable
mint fable
#

Yes, I'm creating the account/login on client side.

#

I was able to successfully post on the database and create an auth user

#

Sorry for the confusion. I am using the client SDK, I'm pretty sure

mint fable
#

AppwriteException: User (role: guests) missing scope (account) [SOLVED]

junior hare
#

Is this problem solved as well?
I got a same problem when I followed a video from youtube. Is it related to SDK API?
https://www.youtube.com/watch?v=_W3R2VwRyF4&t=7415s

Build a modern social app with a stunning UI with a native mobile feel, a special tech stack, an infinite scroll feature, and amazing performance using React JS, Appwrite, TypeScript, and more.

⭐ Appwrite: https://apwr.dev/JSMastery

🌟 Become a top 1% Next.js 14 developer in only one course: https://jsmastery.pro/next13

📚 Materials/References:...

▶ Play video
vast hedge
wanton bronze
#

Go back to your AuthContext and make sure your useEffect looks like this:

useEffect(() => {
    if (
      localStorage.getItem("cookieFallback") === "[]" ||
      localStorage.getItem("cookieFallback") === null
    ) {
      navigate("/sign-in");
    }
    checkAuthUser();
  }, []);
wanton bronze
junior hare
#

I wonder there is any wrong with api call?

import { INewUser } from "@/types";
import { ID, Query } from "appwrite";
import { account, appwriteConfig, avatars, database } from "./config";

export async function createUserAccount(user: INewUser) {
  try {
    const newAccount = await account.create(
      ID.unique(),
      user.email,
      user.password,
      user.name
    );

    if (!newAccount) throw Error;

    const avatarUrl = avatars.getInitials(user.name);
    const newUser = await saveUserToDB({
      accountId: newAccount.$id,
      name: newAccount.name,
      email: newAccount.email,
      username: user.username,
      imageUrl: avatarUrl,
    });

    return newUser;
  } catch (error) {
    console.log(error);
    return error;
  }
}

export async function saveUserToDB(user: {
  accountId: string;
  name: string;
  email: string;
  imageUrl: URL;
  username?: string;
}) {
  try {
    const newUser = await database.createDocument(
      appwriteConfig.databaseId,
      appwriteConfig.userCollectionId,
      ID.unique(),
      user
    );

    return newUser;
  } catch (error) {
    console.log(error);
  }
}

export async function signInAccount(user: { email: string; password: string }) {
  try {
    const session = await account.createEmailSession(user.email, user.password);
    return session;
  } catch (error) {
    console.log(error);
  }
}

export async function getCurrentUser() {
  try {
    const currentAccount = await account.get();

    if (!currentAccount) throw Error;

    const currentUser = await database.listDocuments(
      appwriteConfig.databaseId,
      appwriteConfig.userCollectionId,
      [Query.equal("accountId", currentAccount.$id)]
    );

    if (!currentUser) throw Error;

    return currentUser.documents[0];
  } catch (error) {
    console.log(error);
    return null;
  }
}

#

API with appwrite

import { Client, Account, Databases, Storage, Avatars } from "appwrite";

export const appwriteConfig = {
  projectId: import.meta.env.VITE_APPWRITE_PROJECT_ID,
  url: import.meta.env.VITE_APPWRITE_URL,
  databaseId: import.meta.env.VITE_APPWRITE_DATABASE_ID,
  storageId: import.meta.env.VITE_APPWRITE_STORAGE_ID,
  userCollectionId: import.meta.env.VITE_APPWRITE_USER_COLLECTION_ID,
  postCollecitonId: import.meta.env.VITE_APPWRITE_POST_COLLECTION_ID,
  savesCollectionId: import.meta.env.VITE_APPWRITE_SAVES_COLLECTION_ID,
};

export const client = new Client();
client.setProject(appwriteConfig.projectId);
client.setEndpoint(appwriteConfig.url);

export const account = new Account(client);
export const database = new Databases(client);
export const stroage = new Storage(client);
export const avatars = new Avatars(client);
tranquil raptor
#

[SOLVED] AppwriteException: User (role: guests) missing scope (account)

vast hedge
tranquil raptor
mint fable