Back

Error 401 Unauthorised

  • 0
  • Web
ZachHandley
2 Nov, 2023, 19:14

then use account.get but wrap it in a try/catch

TL;DR
The user is experiencing an Error 401 Unauthorised. They have defined certain functions in the auth_service file and are using try/catch statements to handle potential errors. The user wants to prevent the error from showing in the console, so it doesn't appear to the user. There is a recommended isLoggedIn function that checks if the user is logged in based on the store value. It suggests using account.get and wrapping it in a try/catch statement. No solution has been provided in the support thread.
ZachHandley
2 Nov, 2023, 19:14

so for instance

ZachHandley
2 Nov, 2023, 19:14
TypeScript
const isLoggedIn = computed(() => {
    const maybeLocal = localStorage.getItem("pvauser");
    if (maybeLocal) {
      $user.value = JSON.parse(maybeLocal);
      return true;
    } else if ($user.value) {
      return true;
    } else {
      return false;
    }
  });
ZachHandley
2 Nov, 2023, 19:14

that's a basic computed to check my store value

ZachHandley
2 Nov, 2023, 19:14

What I would recommend is something like

ZachHandley
2 Nov, 2023, 19:15
TypeScript
const isLoggedIn = async () => {
  try {
    const user = await account.get();
    return user (or true);
  catch (e) {
    return null | undefined;
  }
}
ZachHandley
2 Nov, 2023, 19:16

For a user to register, you simply have to do account.create(ID, email, password)

Igneous
2 Nov, 2023, 19:16

Yea my getUser function is defined like this only

ZachHandley
2 Nov, 2023, 19:16

perfect

ZachHandley
2 Nov, 2023, 19:16

then when they login

ZachHandley
2 Nov, 2023, 19:16

you want to do account.createEmailSession

ZachHandley
2 Nov, 2023, 19:16

That is what creates the session

ZachHandley
2 Nov, 2023, 19:16

that account.get() looks for

ZachHandley
2 Nov, 2023, 19:16

if that session exists, then it won't error and will return the user

Igneous
2 Nov, 2023, 19:17

Ah oke i just don't want the error to show in console, so it doesn't show to the user, but if it won't crash the app I think it's no harm

ZachHandley
2 Nov, 2023, 19:18

If you use try/catch

ZachHandley
2 Nov, 2023, 19:18

it won't error in the console

ZachHandley
2 Nov, 2023, 19:18

I know what you mean, I thought the same exact thing haha

ZachHandley
2 Nov, 2023, 19:18

but try/catch is easy and consistent

ZachHandley
2 Nov, 2023, 19:18

and it works for most so like

ZachHandley
2 Nov, 2023, 19:18

getDocument

ZachHandley
2 Nov, 2023, 19:18

will error if there is no document

Igneous
2 Nov, 2023, 19:18

Defined all these functions in the auth_service file so I think I am going on the right path I just need to recheck the try/catch as u said

ZachHandley
2 Nov, 2023, 19:19

yeah let me know if you need any more help 🙂

Igneous
2 Nov, 2023, 19:20

Sure and thanks for the help guys<:appwriteheart:782571497393750036>

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