Back

[SOLVED] Conditional access to documents

  • 0
  • Web
  • Databases
  • Accounts
  • Cloud
  • Users
fafa
26 Nov, 2023, 13:13

You use either the user cookies, or a JWT if you make a server/function request

TL;DR
The user was seeking help on setting conditional access to documents. They wanted to implement custom responses based on user permissions. The solution suggested was to perform the permissions check in the function and return a custom response if the user does not have the necessary permissions. The user also asked for clarification on the flow of execution and how to obtain the user ID. The solution proposed was to use a JWT token or retrieve the user ID from the /v1/account endpoint. The thread ends with a discussion on whether to allow invalid requests to be placed in the database before running functions.
sswastik02
26 Nov, 2023, 13:13

My idea is that it shouldn't be placed in the database in the first place. And then run functions after, which would be a bit inefficient right ?

fafa
26 Nov, 2023, 13:13

Shouldn't or should?

sswastik02
26 Nov, 2023, 13:14

If the request is invalid that is user_id does not match the user id of the user i.e. User tries to create a document with user_id that violates this condition

sswastik02
26 Nov, 2023, 13:15

so we don't want to encourage that right?

fafa
26 Nov, 2023, 13:16

So, you have multiple options. You can get the userId from /v1/account (see docs using SDK). Then check in your code if the userId is the same or not. If not then fail. You can also use functions, which would be easier and also server-sided.

fafa
26 Nov, 2023, 13:17

For server-sided, you need to send a JWT token to the function, so it "works like a user"

sswastik02
26 Nov, 2023, 13:18

Ok, so I invoke function as a user, and then the function adds it into the collection, is that correct ?

D5
26 Nov, 2023, 13:19

What do you mean?

sswastik02
26 Nov, 2023, 13:23

The appwrite function is executed(https://appwrite.io/docs/products/functions/execution) by the user to add data to the collection. The function is written so that it creates the user_id (from the JWT) in the document.

sswastik02
26 Nov, 2023, 13:23

Is this flow correct ?

D5
26 Nov, 2023, 13:23

And?

D5
26 Nov, 2023, 13:24

Basically it will be something like this:

  • User -> Appwrite -> Function -> Appwrite -> User
D5
26 Nov, 2023, 13:28

All this is performed fast so I don't think there will be any problems. At least in my case execution time is something like 500ms having in mind it does a lot more things apart from creating documents, like sending notifications to all my users, getting user's ID, doing a specific check and creating documents. All of this without being optimized, so in other words it could be improved to something like 400ms or maybe less.

D5
26 Nov, 2023, 13:28

It's Dart runtime so It's compiled. In node or other runtimes maybe it runs a bit slower

sswastik02
26 Nov, 2023, 13:33

You are right, it won't take as much time but, there is no response of status For example, if users are not allowed to delete documents, and if the user tries to delete then, he would receive a 401. This is what I need when user tries to create a document with a mismatch in the user_id (user_id in document does not match the user id of the user)

D5
26 Nov, 2023, 13:33

Function always has permission

D5
26 Nov, 2023, 13:34

If you forbid the permissions to execute the function, you will get a permissions error response

D5
26 Nov, 2023, 13:34

If you do the permissions check in the function you can return a custom response

sswastik02
26 Nov, 2023, 13:36

Sorry, I don't understand what you mean by custom response. Here I see, createExecution and getExecution. https://appwrite.io/docs/references/cloud/client-web/functions Are you asking me to long poll for the status?

ideclon
26 Nov, 2023, 16:13

createExecution() will return whatever you return from the Function (assuming itโ€™s not an async Function)

sswastik02
26 Nov, 2023, 17:02

I see.

sswastik02
26 Nov, 2023, 17:03

Thank you so much @fafa @D5 @ideclon ๐Ÿ™‡โ€โ™‚๏ธ ๐Ÿ™‡โ€โ™‚๏ธ

fafa
26 Nov, 2023, 17:16

If this is solved, please add [Solved] at the beginning of the title, thanks!

sswastik02
27 Nov, 2023, 09:27

[SOLVED] Conditional access to documents

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