-
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: []}
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The find: [iff(context => context.params.authentication, authenticate('jwt'))], |
Beta Was this translation helpful? Give feedback.
The
authenticate
hook is what populates the logged in user. It uses the authentication information fromparams.authentication
to do that. You can check if authentication information has been set by looking atparams.authentication
. So a service method call that allows both authenticated and unauthenticated access would look like this: