0

How to check if a user is logged in Feathers,it seems only when using the authenticate hook will user be populated in the context params. is there a way to check on a non-restricted path like in the find hook below.

export const messages = app => {
app.use('messages', new MessageService(getOptions(app)), {
    methods: ['find', 'get'],
    events: []
})
app.service('messages').hooks({
    around: {
        all: []
    },
    before: {
        all: [],
        // here the user is undefined even if request is done by a logged in user
        find: [iff(isProvider('external'), async cxt => console.log(cxt.params.user))],
        // here the user is defined since only logged in users can access it
        get: [ authenticate('jwt'), iff(isProvider('external'), async cxt => console.log(cxt.params.user))],
    },
    after: {all: []},
    error: {all: []}
})
}
James Z
  • 12,209
  • 10
  • 24
  • 44
ramon22
  • 3,528
  • 2
  • 35
  • 48

1 Answers1

0

Answered by daff, all thanks to him.

quote:-

The authenticate hook is what populates the logged in user. It uses the authentication information from params.authentication to do that. You can check if authentication information has been set by looking at params.authentication. So a service method call that allows both authenticated and unauthenticated access would look like this:

find: [iff(context => context.params.authentication, authenticate('jwt'))]
ramon22
  • 3,528
  • 2
  • 35
  • 48