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
- payment
I’ve never used this service. I remember signing up a long time ago but completely forgot about it. I’ve been receiving emails, but since the payments were fai...
- Problem with Google Workspace at DNS Rec...
Hello, I bought a domain at Namecheap, and Google Workspace used to work there, but now that I switched from Custom DNS to Appwrite's nameservers, it doesn't w...
- change role of a team member in Appwrite
It's not possible to add/change roles of a team meber in Appwrite Frontend. When you click on a member of a team you get forwarded to the configuration page of ...