Back

Create document with permission

  • 0
  • Self Hosted
  • Databases
  • Web
JAV
18 Sep, 2024, 15:04

In my web, I try to create a contact form that allow every visitor can leave a message to specified user.

  • Anyone can create message => Anyone can create a document in my Contact Message Collection
  • In the form, I create an input or dropdown to select the person that is receiver ( In example is User 5c1f88b42259e)
TypeScript
import { Client, Databases, Permission, Role } from "appwrite";

const client = new Client()
    .setEndpoint('<APPWRITE_ENDPONT>')
    .setProject('<PROJECT_ID>');

const databases = new Databases(client);

let promise = databases.createDocument(
    '<DATABASE_ID>',
    '<COLLECTION_ID>',
    {'actorName': 'Chris Evans', 'height': 183},
    [
        Permission.read(Role.user("5c1f88b42259e")),    // User 5c1f88b42259e can view this document
        Permission.delete(Role.user("5c1f88b42259e")),  // User 5c1f88b42259e can delete this document
        Permission.delete(Role.team("admin"))           // Admins can delete this document
    ]
);

promise.then(function (response) {
    console.log(response);
}, function (error) {
    console.log(error);
});

In my Contact Message Collection, I turn on Document security option and set Any can create document.

When submit form, it give Error: User session not found.

Can anyone give some solutions or examples?

TL;DR
Developers are seeking help to create a document with permissions in a contact form on their web. They encountered an "Error: User session not found" when attempting to submit the form. The issue may be due to a missing user session. The provided code snippet demonstrates creating a document with specific permissions using the Appwrite SDK. If you encounter the "User session not found" error, ensure that the user session is properly initialized before submitting the form.
George Sadek
18 Sep, 2024, 15:28

I think in the response it will show you who can create permissions for. It's not possible to create permissions for another user.

JAV
18 Sep, 2024, 15:47

Thank you for response. For anyone come to this topic later, I do some research and found the explanation video

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