Back

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

  • 9
  • Databases
  • Accounts
Mosh Ontong
6 Dec, 2023, 09:41

When I try to login this is the result, but before my login is working properly. I have also User data collection but I already set the permission who can read.

TL;DR
User is encountering a "User (role: guests) missing scope (account) (401)" error when trying to login. They previously had a functioning login page. The user has set the permissions for the User data collection and is unsure why they are receiving this error. They have tried refactoring their methods and are using Flutter. It seems other users have also encountered the same issue. Solution: It is suggested to check if the login request failed and if the create session request also failed. Additionally, ensure that the permissions for the User data collection are set correctly.
Drake
6 Dec, 2023, 09:51

role:guests means you're not logged in. Can you provide more detail about what you're doing and what's going on?

Mosh Ontong
6 Dec, 2023, 09:52

I am trying to login my account

Mosh Ontong
6 Dec, 2023, 09:53
Mosh Ontong
6 Dec, 2023, 09:53
TypeScript
  @override
  Future<UserProfile> signInWithEmailAndPassword({
    required String email,
    required String password,
  }) async {
    try {
      await account.createEmailSession(
        email: email,
        password: password,
      );

      return currentUser;
    } on AppwriteException catch (e, stacktrace) {
      _logger.severe(
        'Failed to sign in user with email: $email',
        e,
      );
      throw LoginException(
        e.message ?? 'Failed to sign in user with email: $email',
        stackTrace: stacktrace,
        provider: AuthProvider.email,
      );
    } catch (e, stacktrace) {
      _logger.severe(
        'Failed to sign in user with email: $email',
        e,
      );
      throw LoginException(
        e.toString(),
        stackTrace: stacktrace,
        provider: AuthProvider.email,
      );
    }
  }
Mosh Ontong
6 Dec, 2023, 09:53
TypeScript
  Future<UserProfile> get currentUser async {
    _logger.info('Getting current user');

    try {
      final user = await account.get();

      _logger.info('Got current user');
      final userProfile = await getUserProfile(userId: user.$id);
      return userProfile.copyWith(
        data: user,
      );
    } on AppwriteException catch (e, stacktrace) {
      _logger.warning('Failed to get current user', e);
      throw AuthException(
        e.message ?? 'No user signed in',
        stackTrace: stacktrace,
      );
    } catch (e, stacktrace) {
      _logger.severe('Failed to get current user', e);
      throw AuthException(
        e.toString(),
        stackTrace: stacktrace,
      );
    }
  }
Mosh Ontong
6 Dec, 2023, 09:53
TypeScript
  @override
  Future<UserProfile> getUserProfile({required String userId}) async {
    try {
      final response = await databases.getDocument(
        databaseId: userCredential.databaseId,
        collectionId: userCredential.collectionId,
        documentId: userId,
      );

      return UserProfile.appwriteParse(response.data);
    } on AppwriteException catch (e, stackTrace) {
      _logger.severe(
        '''
    Failed to get the user information for user $userId''',
        e,
        stackTrace,
      );
      throw ProfileUpdateException(
        '''
    Failed to get the user information for user $userId''',
        stackTrace: stackTrace,
      );
    } 
Mosh Ontong
6 Dec, 2023, 09:54

This was my firsst time to encounter this error, Before my login page is working properly

VincentGe
6 Dec, 2023, 10:02

@Mosh Ontong Hmmm I would take a look at if the login failed for some reason

VincentGe
6 Dec, 2023, 10:03

πŸ‘€ Did the create session request also fail?

Mosh Ontong
6 Dec, 2023, 10:05

I observe that earlier the login is executed successfully I can see the user session in auth page of appwrite console

Mosh Ontong
6 Dec, 2023, 10:06

Since my method returning a user credential collection. I set the permission to any

Mosh Ontong
6 Dec, 2023, 10:06

so I can test if the collection is the root cause

Mosh Ontong
6 Dec, 2023, 10:07

but still I am receiving this error

Mosh Ontong
6 Dec, 2023, 10:08

Now I refactor my method, I am now returning a account.get(); and I receive this error

Mosh Ontong
6 Dec, 2023, 10:09

I am not familiar with this error

Mosh Ontong
6 Dec, 2023, 10:10

btw I am using flutter

Mosh Ontong
6 Dec, 2023, 10:18

I think we have same issue

Mosh Ontong
6 Dec, 2023, 10:35

I always received this exception:

Drake
6 Dec, 2023, 11:21

Flutter web?

Mosh Ontong
6 Dec, 2023, 11:29

flutter window

Mosh Ontong
6 Dec, 2023, 11:43
Mosh Ontong
6 Dec, 2023, 11:43

I found that in my auth console it actually created a session after login

Mosh Ontong
6 Dec, 2023, 11:44

So basicayll if I hot reload my flutter app, it will check first if the user is authenticated by using account.get

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