Only get projects belonging to user

Options

Hi, new to this and I can't figure out how to do this:

I have a table with "users" and a table with "projects". The projects has a reference field to the user, as each project belongs to a user. In my frontend I want to ONLY show the user the projects that belongs to the user. How would I go about setting that up in Xano?

Thanks!

Answers

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options

    Hey @filiphartman - you'll be able to do this by filtering your projects table in the Query to say

    WHERE db projects.user_id = auth ID

    (When the endpoint has authentication on)

    Here's some documentation around restricting access so users can only see things you intend them to:

    https://docs.xano.com/building-features/role-based-access

  • filiphartman
    Options

    Thank you! You mean like this? Is there anything else I need to add? I've added restricted access, but when I try to run the request (I'm using wized). I get back unauthorized although the request includes the auth token of the user.

  • filiphartman
    filiphartman Member
    edited October 2023
    Options

    Okay so I got it working! But right now I have a single user associated, if i change it to "list" and add another user, it won't work. What do I need to do to make it work with multiple users?

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options

    So if you have a list of users, your expression will change.

    WHERE auth:id IN db:projects.user_id

    If projects.user_id is a list (array). This is saying the authenticated user ID is in the array of user IDs.