Back

[SOLVED] AppwriteException: user_unauthorized

  • 0
  • Flutter
  • Functions
  • Accounts
Ipsoka
13 Apr, 2023, 20:05

What am I doing wrong?

I have a function that get trigger for every users.*.create and it does the following:

TypeScript
    Map<String, dynamic> functionData = jsonDecode(req.variables['APPWRITE_FUNCTION_EVENT_DATA']);
    String userId = functionData["\$id"];
    await database.createDocument(
      databaseId: "---database id ---",
      collectionId: "--- collection id ---",
      documentId: userId,
      permissions: [Permission.read(Role.user(userId))],
      data: {
        "code": "--- random code ---"
      },
    );

The document gets created successfully but client side

Client side i'm doing the following:

TypeScript
    User user = await account.get();
    Document document = await databases.getDocument(
      databaseId: "---database id ---",
      collectionId: "--- collection id ---",
      documentId: user.$id,
    );

Which throws:

AppwriteException: user_unauthorized, The current user is not authorized to perform the requested action. (401)

I am logged in as the user the triggered the function, so it should work or no?

TL;DR
The user is encountering an "AppwriteException: user_unauthorized" error when trying to access a document. They are using document level security in their function code and are logged in as the user who triggered the function. They are unsure why they are getting the authorization error. Solution: Check if document level security is enabled on the collection. If it is enabled, ensure that the user has the necessary permissions to read the document.
Drake
13 Apr, 2023, 20:12

Is document level security enabled on the collection?

Ipsoka
13 Apr, 2023, 20:14

Dang! No it wasnt, works now! <:appwritecheers:892495536823861258>

Ipsoka
13 Apr, 2023, 20:15

[SOLVED] AppwriteException: user_unauthorized

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