How to return result that belongs to user and restrict other data being sent in the result in FeathersJS?

49 Views Asked by At

I am new to feathersjs and I've breaking my head on how to restrict users from viewing other's data. For instance from postman when you do find/get(id) http://localhost/users you see list of all the users that are currently registered. However I would only like to return the current users data. From:

    {
    "total": 2,
    "limit": 10,
    "skip": 0,
    "data": [
        {
            "id": 1,
            "email": "[email protected]",
            "auth0Id": null,
            "createdAt": "2020-02-01T23:16:30.833Z",
            "updatedAt": "2020-02-01T23:16:30.833Z"
        },
        {
            "id": 2,
            "email": "[email protected]",
            "auth0Id": null,
            "createdAt": "2020-02-01T23:31:50.904Z",
            "updatedAt": "2020-02-01T23:31:50.904Z"
        }
    ]
}

To:

 {
    "total": 1,
    "limit": 10,
    "skip": 0,
    "data": [
        {
            "id": 1,
            "email": "[email protected]",
            "auth0Id": null,
            "createdAt": "2020-02-01T23:16:30.833Z",
            "updatedAt": "2020-02-01T23:16:30.833Z"
        },
    ]
}

How do I filter result in the after hook function? Can someone please explain.

Answer: Missed a basic step. All the find(), update() etc return result based on parent class. By Customizing find() method in your own class and using Sequlize.findOne({}) you can return the result you desire.

Damn! I was stupid to not read the docs fully.

0

There are 0 best solutions below