Skip to content
Back

Check for account

  • 0
  • Web
  • Cloud
Raspova
16 Dec, 2024, 02:02

In this function, is there a way to check that there's an account connected before calling account.get(), or maybe mute the console.error from Appwrite in prod ? Because I get a 401 error in my console when not connected.

export async function getUser() { try {

TypeScript
    return await account.get();
} catch (error) {
    console.log("Get user failed : user probably not logged in");
    return null;
}

}

I try adding this but I get a new scope error:

const session = await account.getSession('current'); if (!session) { console.log("No active session found"); return null; } I tried some jwt token shenanigan but still i get an error

I need this to get to 100 in my best practice lighthouse test

TL;DR
To prevent a 401 error when there is no connected account before calling account.get(), developers can add a check for an active session like this: ```javascript export async function getUser() { try { const session = await account.getSession('current'); if (!session) { console.log("No active session found"); return null; } return await account.get(); } catch (error) { console.log("Get user failed: user probably not logged in"); return null; } } ``` This change should help avoid the 401 error and improve the best practice rating in Lighthouse tests.
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