#[SOLVED] AppwriteException: User (role: guests) missing scope (account)
54 messages · Page 1 of 1 (latest)
What's your code?
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.
Seems you're getting an account, so seems it's an issue with your code 🤔
Also, from what I see in the screenshots, there's a previous error?
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.
But seems you're getting an account, not a database document/s
I set up my code so that it would create an Auth account, AND post to the db (under the Users collection)
Seems the problem is not in the db part, but in the Auth part. That's why I asked to see the related code, not all
Agreed, it has something to do with my auth. Let me see if I can post a snippet.
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."})
}
}
You can't do that client side
Are you using an API key there?
I think you're using server SDK instead of client SDK, but anyways, where did CreateUserAccount came from?
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;
}
}
Why you're performing account creation server side?
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
I'm following a YouTube tutorial, and this is how they're setting it up. I could be mistaken, it could be client side.
Client side is better
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
Following this: https://appwrite.io/docs/products/auth/accounts
AppwriteException: User (role: guests) missing scope (account) [SOLVED]
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:...
hey, what was the solution to this problem?
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();
}, []);
👆
But mine is always complain about appwrite scope
I've added this as well.
useEffect(() => {
if (
localStorage.getItem("cookieFallback") === "[]" ||
localStorage.getItem("cookieFallback") === null
) {
navigate("/sign-in");
}
checkAuthUser();
}, []);
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);
[SOLVED] AppwriteException: User (role: guests) missing scope (account)
thank you, but this is not the solution to my problem. i have the same code.
please create a separate post
The problem wasn't due to the code, for me.
It was due to permissions in the appwrite dashboard.