I just started using AppWrite.
I'm looking for a way to check whether there is a running session on page load and load the user data if the session exist.
The only way I found is to use the the following:
async restoreSession() {
const account = new Account(appWriteClient);
try {
sessionModel.userData = await account.get();
} catch (error) {
//well, not an error as such, just not restoring the session
}
}
The issue is that when no session is available the API call will generate a 401 error that will display in the console. Not very graceful.
Is there a way to do an offline check, short of checking for the presence of localStorage.get('cookieFallback')
?
Thanks
Best way is to do an api call, but you could skip the check if there is no cookie/local storage fallback cookie at all on the client.
You could create a function to check for a valid session the same way but return 200 status with a response field that contains the session data.
I think it is pretty normal to return 401 if not authorized though as far as rest api goes. hope that helps
Thanks for the answer.
Yes, it makes sense for the API to return a 401 because the account.get()
call is meant to be an authenticated one.
What I was expecting to find is an SDK method to check for existing sessions without unnecessary API calls.
Checking localStorage
myself doesn't seem future proof as the AW team might change the SDK's inner workings.
The thing is, making an API call is the only way to verify. What if the session was deleted/revoked? The server is the only source that can confirm that
I get that. I think that a function that mitigates the 401 as suggested above is the way to go for now as I think that an error in the browser's console, although justified "technically", is not neat. It might be just me, but when I deploy my apps I don't like to see the console spitting red if it's not really a sign of malfunctioning 😉
BTW, I really like AppWrite so far, and I love the fact that comes with the option of self hosting with Docker out of the box. Way to go!
Recommended threads
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...
- Deploy function not working - 503
Hellon i get this error message, when i try to deploy a new version of a function <html><body><h1>503 Service Unavailable</h1>No server is available to handle...
- Error When load the website
Hi, I am getting this error whenever I reload my website please help me, I am using react Error: ** GET https://cloud.appwrite.io/v1/account 401 (Unauthoriz...