Back

Getting error "[AppwriteException: User (role: guests) missing scope (account)]" when trying signin

  • 0
  • Databases
  • Auth
  • Web
darkk6810
3 Jul, 2024, 14:35

Hey guys,

I searched the docs, but didn't find a solution. Basically I am making a CRUD app, using react native and email auth in app write. I am getting an error: ERROR [AppwriteException: User (role: guests) missing scope (account)]

And from what I can tell, it is because I am using the account method before intiializing a session in order to check active sessions. So, what's a way to check active sessions on a device without using account.get()? I want to redirect user based on active session type to different screens.

Please help

here is the code snippet that I think is relevant: const handleSignIn = async () => { try { // First check if there is already an active session const sessions = await account.listSessions(); if (sessions.total > 0) { // If session exists, get user details const user = await account.get(); const userProfile = await databases.getDocument('', '', user.$id); navigateBasedOnRole(userProfile); } else { // If no session, create one await account.createEmailPasswordSession(email, password); const user = await account.get(); const userProfile = await databases.getDocument('', '', user.$id); navigateBasedOnRole(userProfile); } } catch (error) { console.error(error); Alert.alert("Error", error.message); } };

TL;DR
Developers are encountering the error "[AppwriteException: User (role: guests) missing scope (account)]" when trying to sign in due to using the account method before initializing a session. To avoid this error, handle the error when using account.get() to check for active sessions on a device. Update the handleSignIn function to include error handling and redirection based on the active session type.
Kenny
3 Jul, 2024, 14:39

You use account.get

TypeScript
const getCurrentUser() {
  try {
    return account.get();
  } catch (err) {
    return null;
  }
}

...

const user = getCurrentUser();

if(!user) {
  redirect('sign-in');
}
darkk6810
3 Jul, 2024, 14:43

Yes, I tried account.get() earlier but to no avail, it showed the same error

Kenny
3 Jul, 2024, 14:44

yes, it will show the error if there is no user, that is what it should do

Kenny
3 Jul, 2024, 14:44

catch the error and handle it

darkk6810
3 Jul, 2024, 14:44

I can share my gh repo if you want: https://github.com/shafinshaikh/garageappv2

Kenny
3 Jul, 2024, 14:44

I don't need it

Kenny
3 Jul, 2024, 14:45

account.get() should throw an error if no one is logged in. You catch that error and handle it appropriately.

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