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

How to extract token from request ? #62

Closed bertho-zero closed 6 years ago

bertho-zero commented 6 years ago

Is there a better way than that to get the token from the request? Or ideally recover the user without protecting the route as does the express middleware?

req.feathers.token = app.passport._strategies.jwt._jwtFromRequest(req);
Example ```js app.use('/graphql', (req, res, next) => { req.feathers.token = app.passport._strategies.jwt._jwtFromRequest(req); next(); }, graphqlExpress(({ feathers }) => ({ schema, context: { app, feathers } })) ); ```
bertho-zero commented 6 years ago
// Express async middleware
const wrap = fn => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);

wrap(async (req, res, next) => {
  const { data } = await app.authenticate('jwt')(req);
  req.feathers.user = data.user;
  req.feathers.authenticated = !!data.user;
  next();
})