Back

[SOLVED] Add user permission in the document created using functions

  • 0
  • Databases
  • Flutter
  • Functions
Hemish11
21 Jul, 2023, 16:58

Im really sorry but its actually under NDA so wont be possible

TL;DR
The user had a support query regarding adding user permission in a document created using functions. They were able to solve it by providing the `write` permission. However, they encountered an issue with duplicated permissions in the database. They were advised not to include the `create` permission and received an error message stating that the permission "create" is not allowed. The user made some code changes and received a new error related to encoding. They were advised to change the variable name to `req` instead of `res` and asked to share the stacktrace of the error. The user made the changes and requested if it was enough. They then
Hemish11
21 Jul, 2023, 16:58

lemme try sharing code by removing some part and so

Hemish11
21 Jul, 2023, 17:01
TypeScript
import 'dart:convert';

import 'package:dart_appwrite/dart_appwrite.dart';


Future<void> start(final req, final res) async {
  try {
    final client = Client()
        .setEndpoint('...')
        .setProject(Constants.projectId)
        .setKey('...')
        .setSelfSigned(status: true);

    final database = Databases(client);

    final payload = req.payload;
    final userId = res.variables['APPWRITE_FUNCTION_USER_ID'];

    final ... = await database.createDocument(
      databaseId: '...',
      collectionId: '...',
      documentId: '...',
      permissions: [
        Permission.read(Role.user(userId)),
        Permission.update(Role.user(userId)),
        Permission.delete(Role.user(userId)),
        Permission.create(Role.user(userId)),
      ],
      data: {...}
    );

  } catch (e) {
    res.json({'error': e});
  }
}
Hemish11
21 Jul, 2023, 17:01

Would this be enough?

Binyamin
21 Jul, 2023, 17:03

Can you add st to the catch and share it?

TypeScript
  } catch (e,st) {
    res.json({'error': e,'st':st});
  }
Binyamin
21 Jul, 2023, 17:03

Oh Wait a sec

Binyamin
21 Jul, 2023, 17:04

Can you change it to req instead of res?

TypeScript
-res.variables['APPWRITE_FUNCTION_USER_ID'];
+req.variables['APPWRITE_FUNCTION_USER_ID'];
Hemish11
21 Jul, 2023, 17:04

lemme try that

Hemish11
21 Jul, 2023, 17:05

Nope

Hemish11
21 Jul, 2023, 17:06

but now it is giving this error

Hemish11
21 Jul, 2023, 17:06

Converting object to an encodable object failed: Instance of 'AppwriteException'

Binyamin
21 Jul, 2023, 17:06

With what stacktrace?

Hemish11
21 Jul, 2023, 17:06

And upon looking at logs of appwrite

Hemish11
21 Jul, 2023, 17:06

Message: Invalid permissions: Permission "create("user:64ac3b8cf403a8b2ab32")" is not allowed. Must be one of: read, update, delete, write.

Hemish11
21 Jul, 2023, 17:06

this is the exact error

Binyamin
21 Jul, 2023, 17:06

Yes, you shouldn't put create one You can see in the example

Binyamin
21 Jul, 2023, 17:07

Here

Hemish11
21 Jul, 2023, 17:10

Well, that did worked, but now the permissions in db are duplicated somehow

Hemish11
21 Jul, 2023, 17:10

"read(user:64ac3b8cf403a8b2ab32)", "update(user:64ac3b8cf403a8b2ab32)", "delete(user:64ac3b8cf403a8b2ab32)", "update(user:64ac3b8cf403a8b2ab32)", "delete(user:64ac3b8cf403a8b2ab32)"

Hemish11
21 Jul, 2023, 17:10

this is the response

Binyamin
21 Jul, 2023, 17:14

Yes, I think you can just give the write permission

TypeScript
      permissions: [
        Permission.write(Role.user(userId)),
      ],

Or write + read

TypeScript
      permissions: [
        Permission.read(Role.user(userId)),
        Permission.write(Role.user(userId)),
      ],
Binyamin
21 Jul, 2023, 17:14

Try it

Hemish11
21 Jul, 2023, 17:15

Lemme try it

Hemish11
21 Jul, 2023, 18:04

Thanks a lot, was able to solve it

Hemish11
21 Jul, 2023, 18:04

[SOLVED] Add user permission in the document created using functions

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