Back

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

  • 1
  • Flutter
Vineet
2 Apr, 2023, 18:00

I am getting this error after logging in through createEmailSession(email, password);

I have a logout button that i use to deleteSession(sessionId: 'current') but apparently the current session seems to be null or not properly created as per the above error.

Any ideas ?

I checked the error and it seems that we need to be logged in but thats what i thought we are doing when we created an email session.

TL;DR
The user is receiving the error "User (role: guests) missing scope (account)" when trying to logout. They suspect that the current session is null or not properly created. To troubleshoot, the user is advised to check if the user is even logged in before attempting to logout. They are provided with code snippets that can be used to check if the user is logged in and then proceed with the logout.
Binyamin
2 Apr, 2023, 18:01

You get the "User (role: guests) missing scope (account)" error when you you run createEmailSession or when you're trying to logout?

Vineet
2 Apr, 2023, 18:01

when i am trying to logout

Binyamin
2 Apr, 2023, 18:02

Okay, So what I will suggest is that you'll check if the user is even logged in

Vineet
2 Apr, 2023, 18:04
TypeScript
 Future<UserModel?> login(String email, String password) async {
    logger.d("Inside login");
    try {
      aw_models.Session session =
          await account.createEmailSession(email: email, password: password);
      aw_models.Document? userDoc = await fetchUserByEmail(email);
      if (userDoc != null) {
        UserModel? user = UserModel.fromJson(userDoc.data);
        return user;
      }
    } catch (e) {
      logger.e('Login failed.');
      rethrow;
    }

    return null;
  }
TypeScript
 Future<void> logout() async {
    try {
      var curr = await account.getSession(sessionId: 'current');
      var response = await account.deleteSession(sessionId: curr.$id);
    } on AppwriteException catch (e) {
      logger.e("Failed to delete session", e.toString());
      throw Fail(
          code: e.code ?? 500,
          message: "Failed to logout the current user",
          response: e.response);
    }
  }

Code snippets for reference

Binyamin
2 Apr, 2023, 18:04

For example

TypeScript
if(await checkIfUserLoggedIn){
  await account.deleteSession('current')

}


async checkIfUserLoggedIn(){

  try{
     await account.get();
     return true;
  } catch (e){
    print(e);
    return false;
  }
}
Vineet
2 Apr, 2023, 18:04

apparently i did that with this

TypeScript
var curr = await account.getSession(sessionId: 'current');
Binyamin
2 Apr, 2023, 18:04

When you're running two await in single catch

TypeScript
      var curr = await account.getSession(sessionId: 'current');
      var response = await account.deleteSession(sessionId: curr.$id);

It will be hard to know which one cause the catch

Binyamin
2 Apr, 2023, 18:05

This will also work

Vineet
2 Apr, 2023, 18:05

both lines give the same error. sorry for confusion, i actually added that just now for debugging

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