Back

AppwriteException: User (role: guests) missing scope (account)

  • 1
  • Android
  • React Native
Kanha
22 Jun, 2024, 15:07

I'm trying to register a new user and then sign them in but got this error: - Error: AppwriteException: User (role: guests) missing scope (account)

Here's the code

// Register user

export async function createUser(email, password, username) { try { const newAccount = await account.create( ID.unique(), email, password, username );

TypeScript
if (!newAccount) throw Error;

const avatarUrl = avatars.getInitials(username);

await signIn(email, password);

const newUser = await databases.createDocument(
  appwriteConfig.databaseId,
  appwriteConfig.userCollectionId,
  ID.unique(),
  {
    accountId: newAccount.$id,
    email: email,
    username: username,
    avatar: avatarUrl,
  }
);

return newUser;

} catch (error) { throw new Error(error); } }

// Sign In

export async function signIn(email, password) { try { await account.deleteSessions(); await account.deleteSession("current"); const session = await account.createEmailPasswordSession(email, password);

TypeScript
return session;

} catch (error) { throw new Error(error); } }

// Get Account export async function getAccount() { try { const currentAccount = await account.get();

TypeScript
return currentAccount;

} catch (error) { throw new Error(error); } }

// Get Current User

export async function getCurrentUser() { try { const currentAccount = await getAccount(); if (!currentAccount) throw Error;

TypeScript
const currentUser = await databases.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; } }

Any insights or suggestions on how to fix this would be greatly appreciated! Thanks in advance!

TL;DR
Error message "AppwriteException: User (role: guests) missing scope (account)" indicates that the current session user is not authenticated. The issue is likely with setting the correct scopes during user registration and sign-in. Make sure to create and maintain a valid session for the user throughout the process. Avoid deleting sessions unnecessarily. Additionally, check the code for missing scopes or incorrect permissions during the user authentication process. This can help resolve the error. Solution: - Review the scope settings for the user role during registration and sign-in. - Avoid unnecessary deletion of sessions. - Ensure proper authentication and permission handling during user operations.
be4st
22 Jun, 2024, 15:19

please use code formatting to make it more readable.

use back ticks

be4st
22 Jun, 2024, 15:21

AppwriteException: User (role: guests) missing scope (account)

this error means that current session user is not authenticated

be4st
22 Jun, 2024, 15:23

in sign in function you dont have to delete sessions on server side. you can directly create new session.

can u specify where the error is coming from??

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