Back

[SOLVED] AppwriteException: User (role: guests) missing scope (account)

  • 0
  • Self Hosted
  • Web
Ruboka
18 Dec, 2023, 05:20

nothing changed. I am still wondering about additional cors setting that I could have messed up

TL;DR
[SOLVED] The user is missing the 'account' scope in the Appwrite backend. The catch block is not working to handle the exception. There is no CORS issue, as a CORS error would be displayed. The console shows a 401 Unauthorized response. It is recommended to check for null or empty user IDs and ensure that the necessary scopes are set correctly in the backend.
ideclon
18 Dec, 2023, 05:20

If CORS were the issue, you wouldn't be getting any response. But you are receiving a response from Appwrite

Drake
18 Dec, 2023, 05:22

Null or user makes much more sense to me. Otherwise, you have to check if an id is an empty string or something like that

Ruboka
18 Dec, 2023, 05:24

okay, since I am wondering since I do get a 401 response in the console

TypeScript
06:20:59.845 XHRGET
http://localhost/v1/account
[HTTP/1.1 401 Unauthorized 6ms]    
GET
    http://localhost/v1/account
Status
401
Unauthorized
VersionHTTP/1.1
Übertragen4,00 kB (8,45 kB Größe)
Referrer Policystrict-origin-when-cross-origin
DNS-AuflösungSystem        
    Access-Control-Allow-Credentials
        true
    Access-Control-Allow-Headers
        Origin, Cookie, Set-Cookie, X-Requested-With, Content-Type, Access-Control-Allow-Origin, Access-Control-Request-Headers, Accept, X-Appwrite-Project, X-Appwrite-Key, X-Appwrite-Locale, X-Appwrite-Mode, X-Appwrite-JWT, X-Appwrite-Response-Format, X-Appwrite-Timeout, X-SDK-Version, X-SDK-Name, X-SDK-Language, X-SDK-Platform, X-SDK-GraphQL, X-Appwrite-ID, X-Appwrite-Timestamp, Content-Range, Range, Cache-Control, Expires, Pragma
    Access-Control-Allow-Methods
        GET, POST, PUT, PATCH, DELETE
    Access-Control-Allow-Origin
        http://localhost:5173
    Access-Control-Expose-Headers
        X-Fallback-Cookies
    Cache-Control
        no-cache, no-store, must-revalidate
    Content-Encoding
        br
    Content-Length
        2990
    Content-Type
        application/json; charset=UTF-8
    Date
        Mon, 18 Dec 2023 05:20:59 GMT
    Expires
        0
    Pragma
        no-cache
    Server
        Appwrite
    X-Content-Type-Options
        nosniff
    X-Debug-Fallback
        true
    X-Debug-Speed
        0.0024588108062744
        
    Accept
        */*
    Accept-Encoding
        gzip, deflate, br
    Accept-Language
        de,en-US;q=0.7,en;q=0.3
    Connection
        keep-alive
    content-type
        application/json
    Host
        localhost
    Origin
        http://localhost:5173
    Referer
        http://localhost:5173/
    Sec-Fetch-Dest
        empty
    Sec-Fetch-Mode
        cors
    Sec-Fetch-Site
        same-site
Ruboka
18 Dec, 2023, 05:24

okay will change it later . Thank you for your input

Drake
18 Dec, 2023, 05:25

What makes you think there's a cors problem?

ideclon
18 Dec, 2023, 05:25

The User (role: guests) missing scope (account) in the Exception is coming from the Appwrite backend

Drake
18 Dec, 2023, 05:26

Which is expected if you're not logged in

Ruboka
18 Dec, 2023, 05:26

just guessing around.

ideclon
18 Dec, 2023, 05:26

Just pointing out that it can't be CORS

Drake
18 Dec, 2023, 05:27

You would get a CORS error if there's a CORS problem so if you're not seeing that, there's no CORS problem

Ruboka
18 Dec, 2023, 05:28

okay. but why does my catch block not work for the exception?

Drake
18 Dec, 2023, 05:29

What's your latest code now?

Ruboka
18 Dec, 2023, 05:37

this was the attempt removing all other code from a the catch block with account.get inside the context itself is still the same

TypeScript
export async function getCurrentUser() {
  var activeAccount = null;
  try {
    activeAccount = await account.get();
    if (!activeAccount) {
      throw new Error("get active Account failed");
    }
    console.log("activeAccount", activeAccount.$id);
  } catch (error: any) {
    console.log("account.get  error: " + error);   
    return error as AppwriteException;
  }
  try {
    const activeUser = await databases.listDocuments(
      conf.APPWRITE_DATABASE_PLANER,
      conf.APPWRITE_USERCOLLECTION_ID,
      [Query.equal("accountID", activeAccount.$id)]
    );
    if (!activeUser) {
      throw new Error("User not found in Collection");
    }
    console.log("activeuser", activeUser);
    return activeUser.documents[0];
  } catch (error: any) {
    console.log("getcurrentUser  error: " + error);
    return error as AppwriteException;
  }
}
Drake
18 Dec, 2023, 05:39

I still think you need to clean this up based on what I said earlier...but anyways, please share a screenshot of the logs from the browser

Ruboka
18 Dec, 2023, 05:44

the double log entrys come from strict mode

Drake
18 Dec, 2023, 05:45

It's going to the catch block...that log is from the catch block...

Ruboka
18 Dec, 2023, 05:45

oh ...

Ruboka
18 Dec, 2023, 05:46

I am far over my uptime. I think I need to go to sleep

Ruboka
18 Dec, 2023, 05:46

but the attempt to call the get() triggers the error log?

Drake
18 Dec, 2023, 05:47

Yes. Whenever there's a network request with a 400+ status code, the browser will log it. Regardless of whether you catch it or not

Ruboka
18 Dec, 2023, 05:49

hmm okay. I understand that I wasted your time, but I am thankfull for your help.

Drake
18 Dec, 2023, 05:49

No worries at all. Nothing is wasted as long as you've learned something

ideclon
18 Dec, 2023, 05:56

If your issue has been solved, you can mark this post as closed by adding “[SOLVED]” to the beginning of the title

Ruboka
18 Dec, 2023, 06:09

[SOLVED] AppwriteException: User (role: guests) missing scope (account)

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