Feathersjs :my hook is failing(loosing context) once i chained it with an other in before function

190 Views Asked by At

I have 2 functions(authenticate, restrictAccess) in the before hook (get) and i want to chain them together.But the restrictAccess is being executed twice(by the second round it has lost the context) When i remove the authenticate , the restrictAccess works as expected.

Here is my hook

module.exports = {
  before: {
    all: [],
    get: [authenticate('jwt'), restrictAccess()],
....

But when i remove authenticate as this

module.exports = {
  before: {
    all: [],
    get: [ restrictAccess()],
....

restrictAccess works as expected

1

There are 1 best solutions below

2
On

The only reason I could think of is that you maybe using the hooks on the users-service.

If this is the case the authenticate-hook probably internally calls the user service to set params.user to the requesting user, which would cause the two calls for the restrictAccess-hook.

A possible fix would be to ignore all internal calls in your restrictAccess-hook:

module.exports = context => {
  if (context.params && context.params.provider) {
    // put restriction logik here....
  }

  return context;
}