Back

Check user session on web page load without API calls

  • 0
  • General
  • Web
CodeSnaporaz
9 May, 2023, 06:30

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:

TypeScript
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

TL;DR
The user is looking for a way to check if there is a session on page load without making unnecessary API calls. They are currently using an API call to check for a session, but it generates a 401 error when no session is present. They want a more graceful solution and wonder if there is a method in the SDK to check for sessions without making an API call. One suggestion is to check for the presence of `localStorage.get('cookieFallback')`, but the user is concerned about future changes to the SDK. The suggested solution is to create a function that checks for a valid session and returns a 200 status with the session data
jSnake🐍🕊
9 May, 2023, 08:58

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

CodeSnaporaz
9 May, 2023, 22:11

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.

Drake
9 May, 2023, 22:13

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

CodeSnaporaz
9 May, 2023, 22:19

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 😉

CodeSnaporaz
9 May, 2023, 22:20

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!

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