Back

AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401)

  • 0
  • Flutter
Binyamin
7 Sep, 2023, 12:37

Before loggin out you can check if the user is even logged in by running the account.get() inside try catch

TL;DR
Issue: `AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401)` occurs when trying to log in. Possible Solution: - Modify the flow of the login process. Use the `success` and `failed` URL parameters when calling `createOAuth2Session` to ensure the session flow is maintained. Example code provided. - Catch any errors using try-catch blocks when calling `createOAuth2Session`. - Check if user is logged in before logging out by running `account.get()` inside try-catch. - Verify if `account.get()` is being called after logging in.
meroo36
7 Sep, 2023, 13:18

yes I am loggedin before I logout

Binyamin
7 Sep, 2023, 13:19

In between you're also required to get the session by using account.get() Can you test when you're using the account.get() right after login?

meroo36
7 Sep, 2023, 13:19

I just tried to login and it took 5 tries to login

meroo36
7 Sep, 2023, 13:20

after 5 tries it worked but other 4 gave the error

meroo36
7 Sep, 2023, 13:20
TypeScript
  logout([bool deleteSessions = true]) async {
    state = const AuthState(isLoggedIn: false);
    if (deleteSessions) {
      Client client = ref.read(appwriteClientProvider);
      Account account = Account(client);
      try {
        var acc = await account.get();
        await account.deleteSessions();
      } catch (e) {
        log(e.toString());
      }
    }
  }
Binyamin
7 Sep, 2023, 13:21

What was the error when you tried to log in?

meroo36
7 Sep, 2023, 13:21

AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401)

Binyamin
7 Sep, 2023, 13:24

When login?

meroo36
7 Sep, 2023, 13:25

yes I get this error when I try to login not try to logout

meroo36
7 Sep, 2023, 13:27
TypeScript
await account.createOAuth2Session(provider: 'apple');
TypeScript
  login() async {
    try {
      Client client = ref.read(appwriteClientProvider);
      Account account = Account(client);

      var appwriteUser = await account.get();

    } catch (e) {
      log("auth_provider Login error: $e");
      Sentry.captureException(e);
    }
  }```
Binyamin
7 Sep, 2023, 13:28

Where is the login it self?

Binyamin
7 Sep, 2023, 13:28

You still need to create the session

Binyamin
7 Sep, 2023, 13:28
meroo36
7 Sep, 2023, 13:29

I use oauth2

Binyamin
7 Sep, 2023, 13:29

Can you share the code flow

meroo36
7 Sep, 2023, 13:29

okay

meroo36
7 Sep, 2023, 13:30
TypeScript
        AppleAuthButton(
          onPressed: () async {
            await account.createOAuth2Session(provider: 'apple');
            return _completeLogin();
          },
          themeMode: ThemeMode.light,
        ),
TypeScript
  void _completeLogin() async {
    await ref.read(authProvider.notifier).login();
  }
TypeScript
  login() async {
    try {
      Client client = ref.read(appwriteClientProvider);
      Account account = Account(client);
      var appwriteUser = await account.get();

      state = AuthState(
          isLoggedIn: true,
          userId: appwriteUser.$id,
          fullName: appwriteUser.name,
          email: appwriteUser.email,
          kollektorId: kUser["id"]);
    } catch (e) {
      log("auth_provider Login error: $e");
      Sentry.captureException(e);
    }
  }
Binyamin
7 Sep, 2023, 13:47

Can you add try catch here

TypeScript
    AppleAuthButton(
          onPressed: () async {
            try {
              await account.createOAuth2Session(provider: 'apple');
              return _completeLogin();
            } catch (e) {
              // log here
            }
          },
          themeMode: ThemeMode.light,
        ),
meroo36
7 Sep, 2023, 14:15

i did

meroo36
7 Sep, 2023, 14:16

it doesnt catch anything there even there is a general_unauthorized_scope error

Guille
7 Sep, 2023, 14:30

createOAuth2Session doesn't return the session, you need to add your logic in the success URL

meroo36
8 Sep, 2023, 09:27

i believe it doesnt explain the inconsistency with the login

meroo36
8 Sep, 2023, 09:27

it only happens time to time

Guille
8 Sep, 2023, 15:49

Just think this: when you start createOAuth2Session you go to a new page, this is a third party page, appwrite can't follow the state, that is why you need to continue your flow with the success and failed URL. It maybe works time to time because a previous session I'm not sure, but if you change your flow, it will always work

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