feathersjs-ecosystem / authentication-jwt

[MOVED] JWT authentication strategy for feathers-authentication using Passport
https://github.com/feathersjs/feathers
MIT License
30 stars 10 forks source link

Possible to return user id in the payload when authenticate? #13

Closed chuan137 closed 7 years ago

chuan137 commented 7 years ago

currently, I got an object with {accessToken} when authenticate successfully. it would be handy if the user's id is also returned for loading more info from the server. maybe it is already implemented?

ekryski commented 7 years ago

@chuan137 the userId is automatically included in the JWT payload. To get access to it you just need to do on the client is:

feathersClient.authenticate({
  strategy: 'local',
  email: 'admin@feathersjs.com',
  password: 'admin'
})
.then(response => {
  console.log('Authenticated!', response);
  return feathersClient.passport.verifyJWT(response.accessToken);
})
.then(payload => {
  console.log('JWT Payload', payload);
  return feathersClient.service('users').get(payload.userId);
})
.then(user => {
  feathersClient.set('user', user);
  console.log('User', client.get('user'));
})
.catch(function(error){
  console.error('Error authenticating!', error);
});