RaspovaOriginal post
Rank 1: Thread
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 {
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
Summary
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.