question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filter join request to return only data of current user

See original GitHub issue

Currently I got his in my Controller:

@Crud({
  model: {
    type: Broker
  },
  query: {
    join: {
      user: {
        eager: true,
        exclude: ['password']
      }
    }
  }
})
@Controller('broker')
@UseGuards(AuthGuard('jwt'))
export class BrokerController {
  constructor(public service: BrokerService) {}
}

There is a one to many relation between user and broker. I am authenticating the users by a jwt token and I need to filter the data on the id of the user. Is there any way to do this through the Crud configuration?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
michaelyalicommented, Sep 16, 2019

@chriszrc working on something similar that will be built in the lib

2reactions
chriszrccommented, Sep 16, 2019

The interceptor is probably the way to go, but I overrode the getMany method for my initial solution, it does the same thing:

@Override()
  getMany(@ParsedRequest() req: CrudRequest, @PrincipalUser() user:PrincipalUser1) {
    //override the supplied user_id with the user_id of the logged in user (principal)
    req.parsed.filter.find(f=>f.field === 'user_id').value = user.user_id;
    return this.base.getManyBase(req);
  }

the @PrincipalUser annotation is just sugar for extracting the req.user:

export const PrincipalUser = createParamDecorator((data, req) => {
  return req.user as User1;
});

@loginov-rocks I think the way you’re setting filter, you would override any existing filters that were supplied in the request? Probably not what most people want-

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nestjs Crud: Filter join request to return only data of current user
I am authenticating the users by a jwt token and I need to filter the data on the id of the user. Is...
Read more >
How to filter 'Assigned To' to the current user - ServiceNow
Hi All,. I'm currently running into some difficulties and would appreciate some insight. I created a new module that returns.
Read more >
Rest API - SOQL Query : Want to get Only Current user's Record
I do not know how to get User ID for filtering. I have tried like this : "SELECT Id FROM User Where Id=CreatedById"....
Read more >
Filter by status and by current user - Power Platform Community
I only want the user to view their own items submitted and their item status. Currently i have for each gallery to filter...
Read more >
3 Ways To Filter A Power Apps Gallery By The Current User
There are many ways to do it. The easiest way is to get the current user's email and use a filter function to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found