Back

[SOLVED]Getting AppwriteException: general_unauthorized_scope (account) (401) when login.

  • 0
  • General
  • Flutter
  • Accounts
๐ŸœUdon๐Ÿœ
26 Sep, 2023, 12:46

Here is my code, I've narrowed it down to currentUserId = await _account().get(); and the _getUser(); which run the same code during login. //sign in method using email or username

TypeScript
@override
  FutureEither signIn(
      {required String email,
      required String password,
      required BuildContext context}) async {
    try {
      if (isValidEmail(email)) {
        final session = await context.authNotifier
            .createEmailSession(email: email, password: password);
        _currentUser = await _account.get();

        return right(session);
      } else {
        final document = await _db.listDocuments(
          databaseId: AppwriteConstants.databaseId,
          collectionId: AppwriteConstants.usersCollection,
          queries: [
            Query.search('userName', email),
          ],
        );
        final user = UserModel.fromMap(document.documents.first.data);
        final responseWithUserName =
            await context.authNotifier.createEmailSession(
          email: user.email,
          password: password,
        );
        _currentUser = await _account.get();

        return right(responseWithUserName);
      }
    } on AppwriteException catch (e, stackTrace) {
      return left(
          Failure(e.message ?? 'Some unexpected error occurred', stackTrace));
    } catch (e, stackTrace) {
      return left(
        Failure(e.toString(), stackTrace),
      );
    }
  }```
TL;DR
User had been receiving a "general_unauthorized_scope (account) (401)" error when trying to login. The issue was that the `_getUser()` method was being called before the user logged in. To solve it, they commented out the `_getUser()` method from the initializer. They also mentioned that the bug came out of nowhere and caused some trouble. As for other possible solutions, they tried clearing the application data and uninstalling the app, but it didn't help. The user also mentioned that their SMTP server stopped working, but they were unsure if it was related to the issue.
๐ŸœUdon๐Ÿœ
26 Sep, 2023, 12:46
TypeScript
            .createEmailSession```
```dart
Future<bool> createEmailSession({
    required String email,
    required String password,
    bool notify = true,
  }) async {
    _status = AuthStatus.authenticating;
    if (notify) {
      notifyListeners();
    }
    try {
      await _account.createEmailSession(email: email, password: password);
      _getUser(notify: notify);
      return true;
    } on AppwriteException catch (e) {
      _error = e.message;
      _status = AuthStatus.unauthenticated;
      if (notify) {
        notifyListeners();
      }
      return false;
    }
  }```
๐ŸœUdon๐Ÿœ
26 Sep, 2023, 12:47

//code from _getUser()

TypeScript
Future _getUser({bool notify = true}) async {
    try {
      _user = await _account.get();
      _status = AuthStatus.authenticated;
    } on AppwriteException catch (e) {
      _status = AuthStatus.unauthenticated;
      _error = e.message;
    } finally {
      _loading = false;
      if (notify) {
        notifyListeners();
      }
    }
  }```
๐ŸœUdon๐Ÿœ
26 Sep, 2023, 12:53

Oh and isValidEmail is just a regex email checker

Drake
26 Sep, 2023, 13:04

Do you only have one instance of Client?

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:04

Yes

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:05

I made this package https://pub.dev/packages/appwrite_auth_kit local since it's outdated and I want to use the latest flutter and appwrite package 11.0

Drake
26 Sep, 2023, 13:06

Does clearing the application data make a difference?

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:06

I haven't tried

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:07

But it's caused a lot of pain. I accidentally deleted my whole appwrite docker package last night ๐Ÿคฃ

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:07

Just a flutter clean?

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:08

Funny thing is this bug came out of nowhere

Drake
26 Sep, 2023, 13:08

On your device, there might be something in the settings for the app. Or you can uninstall the app

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:08

Also my SMTP server doesn't work anymore not sure if related

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:09

I've uninstalled but that doesn't help let me try to clear the device

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:10

Erased all content and settings from the device, I'll keep you updated ๐Ÿ‘Œ

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:11

I sent the app to Apple for app review and the same thing, they said they recieved a white page when logging. Which is the same bug

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:17

For my platform integrations I selected flutter then ios. Not sure if I should add the ios one or the flutter ios one but since the real ios wanted me to add swift I used the flutter ios platform integration

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:18

Same error after clearing the device

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 13:20

I can only login after a hot reload

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 14:29

I've only seen the error I'm talking about in older github reports

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 14:44

Solved the error, I was right about the _getUser() method it was being called before the user even logged in. All I did was comment it out from the initializer and it works. Here's the code ```dart enum AuthStatus { uninitialized, authenticated, authenticating, unauthenticated, }

class AuthNotifier extends ChangeNotifier { late final Account _account; final Client _client; AuthStatus _status = AuthStatus.uninitialized;

User? _user; String? _error; late bool _loading;

AuthNotifier(Client client) : _client = client { _error = ''; _loading = true; _account = Account(client); //_getUser(); }

Account get account => _account; Client get client => _client; String? get error => _error; bool get isLoading => _loading; User? get user => _user; AuthStatus get status => _status;

Future _getUser({bool notify = true}) async { try { _user = await _account.get(); _status = AuthStatus.authenticated; } on AppwriteException catch (e) { _status = AuthStatus.unauthenticated; _error = e.message; } finally { _loading = false; if (notify) { notifyListeners(); } } }``` Sorry for all the trouble @Steven ๐Ÿ˜…

๐ŸœUdon๐Ÿœ
26 Sep, 2023, 14:45

[SOLVED]Getting AppwriteException: general_unauthorized_scope (account) (401) when login.

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