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
- Update User Error
```ts const { users, databases } = await createAdminClient(); const session = await getLoggedInUser(); const user = await users.get(session.$id); if (!use...
- apple exchange code to token
hello guys, im new here 🙂 I have created a project and enabled apple oauth, filled all data (client id, key id, p8 file itself etc). I generate oauth code form...
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...