Back

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

  • 0
  • Android
  • Flutter
  • Cloud
  • Accounts
tom
30 Dec, 2023, 21:09

Suddenly getting this AppwriteException on my Flutter app. Was not happening yesterday. Only thing that has changed is the WiFi network my laptop is connect to. I am using Appwrite Cloud for Flutter app on an Android emulator. I have reviewed previous posts and attempted solutions including clearing browser cookies + deleted/created a new Android emulator + running flutter clean, flutter pub upgrade combo, toggled email/password on/off in Appwrite console + deleted Appwrite project altogether, create new project and ported over all ids to Flutter app. (I am using Appwrite cloud. I am using Flutter 3.16.5. I am using an Android Emulator (Pixel 6)).

An the below error gets thrown when this function is called.

@override Future<model.User?> currentUserAccount() async { final res = await _account.get(); return res; }

TL;DR
The user is experiencing the "AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (401)" error in their Flutter app. They have tried various solutions such as creating a session and checking for an active session before evaluating an authenticated user. They also tried clearing browser cookies, deleting and creating a new Android emulator, running `flutter clean` and `flutter pub upgrade`, and deleting and recreating the Appwrite project. The user is looking for further assistance to resolve the issue. Solution: The user found a temporary solution by updating their main.dart file with error handling code and using `account.deleteSession("
ideclon
30 Dec, 2023, 21:15

Are you definitely creating a Session? This error generally means that there’s no active Session

tom
30 Dec, 2023, 21:28

My main.dart file evaluates whether or not the current user is authenticated by watching a provider called currentUserAccountProvider. currentUserAccountProvider gives the value returned by the .currentUser() function, which is simply the .get() function in appwrite/account.dart.

final currentUserAccountProvider = FutureProvider((ref) { final authController = ref.watch(authControllerProvider.notifier); return authController.currentUser(); });

Future<model.User?> currentUser() => _authAPI.currentUserAccount();

@override Future<model.User?> currentUserAccount() async { final res = await _account.get(); return res; }

Based on your comment above, perhaps I need to first evaluate if there is an active session at all, before evaluating if there is an active authenticated user?

tom
30 Dec, 2023, 21:52

Oh, okay - starting to piece together the issue. Updated my code to create an anonymous session before calling account.get() and that actually resolves the issue somewhat. Hm. So it seems as though I am calling _account.get() to return the current user for a session that does not exist? But if I call .createAnonymousSession() before calling account.get(), the function does not throw an error.

tom
30 Dec, 2023, 22:07

For anyone who stumbles on this post with a similar error, check out this post: https://discord.com/channels/564160730845151244/1089916961896542218/1089920057129320499

ideclon
30 Dec, 2023, 22:14

Correct. An anonymous session is basically a temporary user

ideclon
30 Dec, 2023, 22:14

It’s probably better to catch the error and handle from there

tom
30 Dec, 2023, 22:24

Silly question - how do you sign out of an anonymous session? My above temporary code created a session but now I can't get out of it 🙃

ideclon
30 Dec, 2023, 22:30

account.deleteSession("current")

tom
30 Dec, 2023, 23:02

SOLVED -- I updated my main.dart with the following error handling and it worked. Error 401 is expected when there is no session and user is not authenticated.

Drake
30 Dec, 2023, 23:44

I would wrap the account.get() with a try/catch. If it's successful, set the user (which means there's an active session) and when the user state updates, the UI would update accordingly. If user is null, the user isn't logged in so show the login page

ideclon
2 Jan, 2024, 16:34

If your issue has been solved, you can mark this post as closed by adding “[SOLVED]” to the beginning of the title

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