Back

How to Ensure a User Can Only Access, Add, and Delete Their Own Favorites in Appwrite

  • 0
  • Self Hosted
  • Auth
  • Databases
mobeit
21 Jan, 2025, 18:39

Hi everyone,

I'm building an app using Appwrite, and I'm working on a feature where users can store their favorite items in a "Favorites" collection. Each favorite entry has fields like userId and itemId, and I've set up user authentication so that users can log in.

My goal is to ensure that each user can only view, add, or delete their own favorites and cannot access or modify the favorites of other users. I understand that Appwrite has a permissions system, but I'm not entirely sure how to configure it in a way that restricts access to the favorites collection based on the userId field.

Does anyone have any experience or advice on how to implement this? I’ve looked into Appwrite's permissions for documents, but I’m struggling with how to securely link each user to their own favorites and prevent them from accessing others' data.

Any guidance on setting up the correct permissions or best practices for this scenario would be greatly appreciated!

Thanks in advance!

TL;DR
Developers are seeking advice on how to ensure users can only access, add, and delete their own favorites in the Appwrite platform. The solution involves using document-level permissions in the favorites collection and adding permissions based on the currentUserId when creating a document. This ensures that each user can only interact with their own favorites data.
Kenny
21 Jan, 2025, 18:48

On the favorites collection toggle on document level permissions. Now when creating a document add these permissions.

TypeScript
await databases.createDocument(
    '<DATABASE_ID>',
    '<COLLECTION_ID>',
    {
      foo: "bar"
    },
    [
Permission.read(Role.user(currentUserId)),
Permission.write(Role.user(currentUserId)),
    ]
);
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