feathersjs-ecosystem / feathers-mongoose

Easily create a Mongoose Service for Feathersjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug in custom route hooks ( authentication )

webafra opened this issue · comments

hi .

I want to create a custom route and authenticate that path.
The following code is written to do this:

=> users.service.js

const { authenticate } = require('@feathersjs/authentication').hooks;

app.use('/users/me/ads', {
    async find (context) {
      const { user } = context;
      console.log('user is:', user);
      context.query.user = user._id;
      return await service._find(context);
    },
  }).hooks({
    before: {
      find: [
        authenticate('jwt')
      ]
    }
  })

But after writing this code, all other ( '/pages' , '/ads' and ... ) routes need authentication!
after remove this authenticate('jwt') on find hook, no problem to other routes ( '/pages', '/ads' ... ) .

what is my problem ?

feathers version: 4.5.3
node js : 13

app.use returns the app object, not the service, so you are initializing an application level hook not a service hooks. Service hooks are initialized like this:

const { authenticate } = require('@feathersjs/authentication').hooks;

app.use('/users/me/ads', {
  async find (context) {
    const { user } = context;
    console.log('user is:', user);
    context.query.user = user._id;
    return await service._find(context);
  },
});

app.service('/users/me/ads').hooks({
  before: {
    find: [
      authenticate('jwt')
    ]
  }
});