Back

[Solved] user_unauthorized in appwrite function using Server Dart SDK and API KEY

  • 0
  • Functions
Binyamin
20 Jul, 2023, 18:29

Don't think so

TL;DR
The user was encountering a "user_unauthorized" error when using the Appwrite function with the Server Dart SDK and API Key. It was discovered that the error was caused by passing an array of JSON objects with IDs instead of a string array of IDs. The solution was to pass an array of IDs when linking. There was also an issue where the user was able to create documents without error but the relation was not established. The cause of this issue was not determined. Additionally, the user encountered a 500 error when trying to update a document with a relation from the "child" side. The cause of this error was not determined.
obiwanzenobi
20 Jul, 2023, 18:53

same problem with jwt - user is not authorized, even when all collections have all permissions available to "any"

obiwanzenobi
20 Jul, 2023, 19:52
obiwanzenobi
20 Jul, 2023, 19:53
obiwanzenobi
20 Jul, 2023, 19:53

removed all other fields from collection, only relation is present

obiwanzenobi
20 Jul, 2023, 20:07

tested other relations and only with twoWay relations there is such problem

Drake
20 Jul, 2023, 21:51

so i just tested and i was able to create fine with an api key:

TypeScript
$ appwrite --json databases createDocument --databaseId "many-to-one" --collectionId "level1" --documentId "level1" --data '{
    "level2": {
        "$id": "level2",
        "level3": {
            "$id": "level3",
            "level4": {
                "$id": "level4",
                "level5": {
                    "$id": "level5"
                }
            }
        }
    }
}'
{
  "$id": "level1",
  "$permissions": [],
  "$createdAt": "2023-07-20T21:50:55.904+00:00",
  "$updatedAt": "2023-07-20T21:50:55.904+00:00",
  "level2": {
    "$id": "level2",
    "$createdAt": "2023-07-20T21:50:55.905+00:00",
    "$updatedAt": "2023-07-20T21:50:55.905+00:00",
    "$permissions": [],
    "level3": {
      "$id": "level3",
      "$createdAt": "2023-07-20T21:50:55.906+00:00",
      "$updatedAt": "2023-07-20T21:50:55.906+00:00",
      "$permissions": [],
      "$databaseId": "many-to-one",
      "$collectionId": "level3"
    },
    "$databaseId": "many-to-one",
    "$collectionId": "level2"
  },
  "$databaseId": "many-to-one",
  "$collectionId": "level1"
}
Success
Drake
20 Jul, 2023, 21:53

here are my collections:

Drake
20 Jul, 2023, 21:55

Let me remove permissions:

Drake
20 Jul, 2023, 21:55

and it still works:

TypeScript
$ appwrite --json databases createDocument --databaseId "many-to-one" --collectionId "level1" --documentId "level1" --data '{
    "level2": {
        "$id": "level2",
        "level3": {
            "$id": "level3",
            "level4": {
                "$id": "level4",
                "level5": {
                    "$id": "level5"
                }
            }
        }
    }
}'
{
  "$id": "level1",
  "$permissions": [],
  "$createdAt": "2023-07-20T21:54:59.591+00:00",
  "$updatedAt": "2023-07-20T21:54:59.591+00:00",
  "level2": {
    "$id": "level2",
    "$createdAt": "2023-07-20T21:54:59.592+00:00",
    "$updatedAt": "2023-07-20T21:54:59.592+00:00",
    "$permissions": [],
    "level3": {
      "$id": "level3",
      "$createdAt": "2023-07-20T21:54:59.593+00:00",
      "$updatedAt": "2023-07-20T21:54:59.593+00:00",
      "$permissions": [],
      "$databaseId": "many-to-one",
      "$collectionId": "level3"
    },
    "$databaseId": "many-to-one",
    "$collectionId": "level2"
  },
  "$databaseId": "many-to-one",
  "$collectionId": "level1"
}
Success
obiwanzenobi
20 Jul, 2023, 22:34

Thanks a lot! Im comparing it to mine and in the meantime I will paste mine:

obiwanzenobi
20 Jul, 2023, 22:34

and insert try:

TypeScript
appwrite --json databases createDocument --databaseId "main_database" --collectionId "content_entry" --documentId "test1" --data '{"name":"test","assignedToCategories":[{"$id":"64b9b41199345ad2a55d"}]}'    
Error The current user is not authorized to perform the requested action.
obiwanzenobi
20 Jul, 2023, 22:36

64b9b41199345ad2a55d is already existing object of type category

obiwanzenobi
20 Jul, 2023, 22:39

@Steven maybe problem is that im trying to insert document with relation from the "child" side?

obiwanzenobi
20 Jul, 2023, 22:46

ok, so its possible to create child or parent of the relation but cant reference it

obiwanzenobi
20 Jul, 2023, 23:02

also now getting 500

TypeScript
 appwrite --json databases updateDocument --databaseId "main_database" --collectionId "category" --documentId "k1" --data '{"content": ["c1"]}' --verbose
AppwriteException [Error]: Server Error
    at Client.call (/opt/homebrew/Cellar/appwrite/HEAD-b461997/libexec/lib/node_modules/appwrite-cli/lib/client.js:209:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async databasesUpdateDocument (/opt/homebrew/Cellar/appwrite/HEAD-b461997/libexec/lib/node_modules/appwrite-cli/lib/commands/databases.js:1353:16) {
  code: 500,
  response: [Object: null prototype] {
    message: 'Server Error',
    code: 500,
    type: 'general_unknown',
    version: '1.3.7',
    file: '/usr/src/code/vendor/utopia-php/database/src/Database/Database.php',
    line: 3090,
    trace: [
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype]
    ]
  }
}
Error Server Error
obiwanzenobi
20 Jul, 2023, 23:04

works from the "child" side

obiwanzenobi
20 Jul, 2023, 23:04
TypeScript
appwrite --json databases updateDocument --databaseId "main_database" --collectionId "content_entry" --documentId "c1" --data '{"assignedToCategories": ["k1"]}' --verbose
{
  "name": "c1",
  "$id": "c1",
  "$createdAt": "2023-07-20T22:59:38.347+00:00",
  "$updatedAt": "2023-07-20T23:03:52.676+00:00",
  "$permissions": [],
  "assignedToCategories": [
    {
      "name": "k1",
      "$id": "k1",
      "$createdAt": "2023-07-20T22:59:46.246+00:00",
      "$updatedAt": "2023-07-20T23:03:52.679+00:00",
      "$permissions": [],
      "$databaseId": "main_database",
      "$collectionId": "category"
    }
  ],
  "$databaseId": "main_database",
  "$collectionId": "content_entry"
}
Success 
obiwanzenobi
20 Jul, 2023, 23:06

no error while creating, but no relation either:

TypeScript
 appwrite --json databases createDocument --databaseId "main_database" --collectionId "category" --documentId "k2" --data '{"name":"k2", "content": ["c1"]}' --verbose
{
  "name": "k2",
  "$id": "k2",
  "$permissions": [
    "read(\"user:64b43b2993b48df0d16b\")",
    "update(\"user:64b43b2993b48df0d16b\")",
    "delete(\"user:64b43b2993b48df0d16b\")"
  ],
  "$createdAt": "2023-07-20T23:05:14.443+00:00",
  "$updatedAt": "2023-07-20T23:05:14.443+00:00",
  "$databaseId": "main_database",
  "$collectionId": "category"
}
Success 
Drake
21 Jul, 2023, 00:08

are you trying to create those nested assignedCategories or link?

obiwanzenobi
21 Jul, 2023, 00:09

link

Drake
21 Jul, 2023, 00:10

to link, you should put an array of ids rather than passing an array of objects

obiwanzenobi
21 Jul, 2023, 07:17

Yes! That was it! I was passing array of json objects with id instead of string array of ids

obiwanzenobi
21 Jul, 2023, 07:17

Thank you Steven, you saved the day again

obiwanzenobi
21 Jul, 2023, 07:17

[Solved] user_unauthorized in appwrite function using Server Dart SDK and API KEY

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