Back

document level permission error

  • 1
  • Flutter
  • Functions
coscino
18 Feb, 2024, 01:28

This is my nodejs, cloud function:

TypeScript
import { Client, Databases, ID, Permission, Role } from 'node-appwrite';

export default async ({ req, res, log, error }) => {

  const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject(process.env.APPWRITE_PROJECT_ID)
    .setKey(process.env.APPWRITE_API_KEY);

  const databases = new Databases(client);

  // You can log messages to the console
  log('Hello, Logs!');

  // If something goes wrong, log an error
  error('Hello, Errors!');

  // Parse the request body to get IDINPUT
  const { IDINPUT } = JSON.parse(req.body);
  log(IDINPUT);

  if (req.method === 'GET') {
    // Send a response with the res object helpers
    return res.send(`hello ${process.env.DOG_NAME}`);
  } else if (req.method === 'POST') {
    try {
      /// Create a document in the specified collection
      const response = await databases.createDocument(
        process.env.APPWRITE_DATABASE_ID,
        process.env.APPWRITE_USERINFO_ID,
        ID.unique(),
        
        {surname: 'dibba'},
        [
          Permission.write(Role.user(IDINPUT)),
          Permission.read(Role.user(IDINPUT)),
        ]
      );

      console.log(response);
      return res.send('document created');

    } catch (err) {
      console.log(err);
      return res.send('document not created, internal error');
    }
  }

  return res.json({
    motto: 'Build like a team of hundreds_',
    learn: 'https://appwrite.io/docs',
    connect: 'https://appwrite.io/discord',
    getInspired: 'https://builtwith.appwrite.io',
  });
};

I know I am parsing IDINPUT correctly because in the log I see the correct user id.

I call this function in my flutter app, where I am logged in with an active email session.

I have document level permission active on my collection. I have set that any user can just create.

My apikey has 2 scopes:

  • read documents
  • write documents
TL;DR
Error message indicates the permissions should be "any" or "guests," not "user" in the function. The solution is to update the function to use `Permission.write(Role.any())` and `Permission.read(Role.any())` instead of `Permission.write(Role.user(IDINPUT))` and `Permission.read(Role.user(IDINPUT))`.
coscino
18 Feb, 2024, 01:28

This is the full error:

TypeScript
Hello, Logs!
65cfcc64a2e0faf8ffb8

----------------------------------------------------------------------------
Unsupported logs detected. Use context.log() or context.error() for logging.
----------------------------------------------------------------------------
AppwriteException [Error]: Permissions must be one of: (any, guests)
    at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:172:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Databases.createDocument (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:1666:16)
    at async Module.default (file:///usr/local/server/src/function/src/main.js:28:24)
    at async execute (/usr/local/server/src/server.js:141:22)
    at async /usr/local/server/src/server.js:158:13 {
  code: 401,
  type: 'user_unauthorized',
  response: {
    message: 'Permissions must be one of: (any, guests)',
    code: 401,
    type: 'user_unauthorized',
    version: '0.12.89'
  }
}

----------------------------------------------------------------------------
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