AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (401)
- 0
- Android
- Flutter
- Cloud
- Accounts
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;
}
Are you definitely creating a Session? This error generally means that there’s no active Session
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?
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.
For anyone who stumbles on this post with a similar error, check out this post: https://discord.com/channels/564160730845151244/1089916961896542218/1089920057129320499
Correct. An anonymous session is basically a temporary user
It’s probably better to catch the error and handle from there
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 🙃
account.deleteSession("current")
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.
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
If your issue has been solved, you can mark this post as closed by adding “[SOLVED]” to the beginning of the title
Recommended threads
- Custom Domains
Hi All, Should be a quick config issue. I'm setting up custom domains on the hosted version. I have verified the domain with the CNAME but appwrite isn't gene...
- custom domain with CloudFlare
Hi all, it seems that CloudFlare has blocked cross-domain CNAME link which made my app hostname which is in CloudFlare, unable to create a CNAME pointing to clo...
- [SOLVED] OAuth With Google & Flutter
Hi all, I'm trying to sign in with google and it all goes swimmingly until the call back. I get a new user created on the appwrite dashboard however the flutte...