Back

[SOLVED] Make users only able to view their own entry in the collection

  • 2
  • Databases
  • Web
Danny
23 May, 2023, 21:00

Hi, im trying to set up a database collection so users from the client-side can only view/interact with their own row, so if they are signed in to the correct account. How can i do this? In similar tools you can set access rules, so if logged in user email is equal to specific row allow access, where is this in appwrite?

TL;DR
To make users only able to view their own entry in the collection, you can follow these steps: 1. Set the collection permission so any user can only create documents. 2. Create a function triggered when a new document is created in the collection. 3. Inside the function, adjust the permissions to allow the user to also read the document. Here is an example of how to implement this in your function: ```ts const currentUserId = req.variables.APPWRITE_FUNCTION_USER_ID ?? null; if (currentUserId !== null) { await databases.createDocument( '[DATABASE_ID]', '[COLLECTION_ID]', {}, [
Binyamin
23 May, 2023, 21:01

You can go one of two ways.

Binyamin
23 May, 2023, 21:05
  1. Set the collection permission so any user can only create documents.

Then, create a function and let to be triggered new document is created in the collection. If for example the collection id is aaaaaa then this would be the right trigger for it databases.*.collections.aaaaa.create

Then inside that function you can adjust the permissions to allow the user to also read the document.

Binyamin
23 May, 2023, 21:09
  1. Move everything to a function. Block any access to your collection And inside your function you can use the variable APPWRITE_FUNCTION_USER_ID to get the user id and then do something like this
TypeScript
 const currentUserId = req.variables.APPWRITE_FUNCTION_USER_ID?? null;

if(currentUserId !== null){
  await databases.createDocument(
    '[DATABASE_ID]',
    '[COLLECTION_ID]',
    {},
    [
        Permission.write(Role.team(currentUserID)),          
    ]
  );
}
Binyamin
23 May, 2023, 21:10

This the correct image for the first solution.

Danny
23 May, 2023, 21:10

👍

joeyouss
25 May, 2023, 18:36

[SOLVED] Make users only able to view their own entry in the collection

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