How to get only data added by current user ( filter by user-specific property )
- 2
- Users
- Databases
- Web
- REST API
I'm using Nextjs14, App Router, and did all crud operations on server-side due to some problems. Now I want to do that users can only fetch their own data. How can I filter user-specific data from collections?
In addition: I'm using ApiKey, Redux-toolkit, and login & password authentication for the login part of the application.
Filter data on server-side using authenticated user ID stored in Redux. Include ID filter in database queries and protect API routes with authorization checks. Keep API key secure on server.
Apply a filter in your database queries to retrieve only documents matching the user ID:
// Example using Appwrite SDK (adjust for your database): const authenticatedUserId = useSelector((state) => state.user.id);
const queries = [ Query.equal('userId', authenticatedUserId), // Filter by user ID // ...other queries ];
const response = await database.listDocuments( collectionId: 'yourCollectionId', queries, );
@arifqayoom39 Thank you so much, understand
Is there other way, like, I read in the docs that if we use JWT it automatically gets own data, maybe with cookies, headers ?
You're right! JWTs, cookies with user IDs, or custom headers with user IDs can also achieve user-specific data access. They all work by identifying the user on the server side and using that information to filter queries. Choose the method that best suits your security and authentication needs. Remember to implement proper authorization checks alongside chosen approach.
Great. Thank you again.
I will try with cookies, if I can't, I will ask
Sure just mention my usersname, so I will get notified
This code is out of date - from before Appwrite had multiple databases per Project.
The correct syntax is database.listDocuments("[DATABASE_ID]", "[COLLECTION_ID]", queries)
umm may b!! Correct me & suggest best answer to him🤗
Are you looking to do this still on the server side, or are you looking to move this to the client side?
And how do you denote what data is "user-specific"?
Recommended threads
- 1:1 relationship doesn’t sync after re-a...
Hi, I’m trying to use a two-way one-to-one relationship. It works fine when I create a record with the relationship set, and it also works when I unset it. But ...
- Failed to create function
Hey everyone 👋 I'm having an issue creating Functions on Appwrite Cloud and I'm not sure if it's a platform bug or something wrong in my project. When I try t...
- Upsert with setting permissions
Hi there, I am using self-hosted appwrite v1.7.4 and trying to use the bulk update stuff that was released with 1.7.x. Unfortunally I found that there is an ser...