Like firebase we can check if there is an active session by FirebaseUser. How can we check on start of my MainActivity that if a user is logged in or not. I am working with java, i have not found any proper solution. Appwrite says to use get() but i am not able to understand how to use it. Any help
Asked
Active
Viewed 871 times
0
-
12:02:56.031 W io.appwrite.exceptions.AppwriteException: User (role: guests) missing scope (account) 12:02:56.032 W at io.appwrite.Client$awaitResponse$2$1.onResponse(Client.kt:490) 12:02:56.032 W at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) 12:02:56.032 W at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) 12:02:56.032 W at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) 12:02:56.032 W at java.lang.Thread.run(Thread.java:1012) 12:02:56.032 I No sessions Found – Deadly Mouse Jan 24 '23 at 06:41
-
above was the error on appwrite – Deadly Mouse Jan 24 '23 at 06:42
2 Answers
2
As you mentioned, in Appwrite, the typical approach is to call account.get()
and if it executes successfully, there is an active session and you can send the user to the home page. If account.get()
throws an exception, there is no active session so you should send the user to the log in page.
Related discussion: https://github.com/appwrite/appwrite/discussions/3938#discussioncomment-3746725

Steven Nguyen
- 452
- 4
- 4
0
Let me give you a short example ;)
Future<User?> getUserIfExists() async {
try {
final user = await this.account.get();
return user;
} on AppwriteException catch (e) {
if (e.code != 401 || e.type != 'general_unauthorized_scope') rethrow;
}
return null;
}
So it just catches the Exception if was thrown only because of a non existing session - otherwise you could not tell if it was something else what caused the exception like "no inet connection"
Hope this helps!

apatrck01
- 89
- 1
- 6