Hi there, I'm relatively new to programming and am following a tutorial on youtube just to get an idea of how this all is suppossed to work. Now Im setting up the back end and I keep getting this error "User (role: guests) missing scopes (["account"])" when I try to login or logout. As far as I can tell the issue comes from account.get() and account.deleteSession("current"). Tried to solve it by googling and asking AI, and Ive figured out a workaround for the login and account.get() but the logout still doesnt work. Can anyone help me resolve this?
#Auth Issue (maybe?)
38 messages · Page 1 of 1 (latest)
Can you share your Auth setup code?
import { createContext, useState } from "react";
import { account } from "../lib/appwrite";
import { ID } from "react-native-appwrite";
export const UserContext = createContext();
export function UserProvider({ children }) {
const [user, setUser] = useState(null);
async function login(email, password) {
try {
console.log("Creating session...");
const session = await account.createEmailPasswordSession(email, password);
console.log("Session created:", session);
// Instead of calling account.get(), use the session's userId
// to set a minimal user object
setUser({
$id: session.userId,
email: session.providerUid,
// Add other fields as needed
});
console.log("Login successful");
} catch (error) {
console.log("login error:", error);
throw Error(error.message);
}
}
async function register(email, password) {
try {
console.log("Creating account...");
const newAccount = await account.create({
userId: ID.unique(),
email: email,
password: password,
});
console.log("Account created successfully:", newAccount);
await login(email, password);
} catch (error) {
console.log("register error:", error);
throw Error(error.message);
}
}
async function logout() {
console.log(user);
try {
console.log(user);
await account.deleteSession("current");
setUser(null);
console.log("Logout successful");
} catch (error) {
console.log("logout error:", error.message);
}
}
return (
<UserContext.Provider value={{ user, login, register, logout }}>
{children}
</UserContext.Provider>
);
}
Is this what you mean?
Yes
There's no problem in you logout code.
The issue is with your login code
so it doenst register the loged in user?
Which doesn't properly creates the session, as a result of which, logout doesn't work
So to fix that, you should write something like this:
await account.createEmailPasswordSession(email, password);
const current = await account.get();
setUser(current);
Instead of:
const session = await account.createEmailPasswordSession(email, password);
setUser({
$id: session.userId,
email: session.providerUid,
// Add other fields as needed
});
but now when I got register I get the same error
LOG login error: [AppwriteException: User (role: guests) missing scopes (["account"])]
LOG register error: [Error: User (role: guests) missing scopes (["account"])]
It creates the account correctly since I can see it on the website that the new user exists but then it still doesnt log in
it also seems like there is a session going on based on the website
Ok. Can you also share your appwrite client's config file?
import 'react-native-url-polyfill/auto';
import { Client, Account, Avatars } from 'react-native-appwrite';
const client = new Client()
.setProject("6942f78a001cb9c1be72")
.setPlatform("dev.fkountras.MonsterMeter")
.setEndpoint("https://fra.cloud.appwrite.io/v1");
export const account = new Account(client)
export const avatars = new Avatars(client)
Is it this?
Yes please do the third step if you haven't already
And if you already did that, can you try clearing browser cache and restart the app?
I didnt clone their git since I had something already set up but I added this
useEffect(() => {
account
.get()
.then(() => {
console.log("✅ Appwrite connected (authenticated)");
})
.catch(() => {
console.log("✅ Appwrite connected (guest)");
});
}, []);
to my main page and it did give
LOG ✅ Appwrite connected (guest)
the step still doesnt show as completed though on the website
Ok. Are you using Expo Go?
Yep!
Try running npx expo run:android
did it still nothing changed on the site
Can you share the logs after running this?
Its alot of text should I just send all of it?
or do you mean I should try to login/register and then look at the logs?
I tried it out and it seems everything is working!!!
Thanks so much mate 😁
So what exactly was the issue?
So your issue is resolved?