Skip to content

Commit

Permalink
feat(jwt): tidying up strategy definition
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Sep 22, 2017
1 parent ad651b9 commit 3e2644f
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions server/modules/users/server/config/strategies/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ const UserService = require('../../services/user.service')
const JwtStrategy = passportJwt.Strategy
const ExtractJwt = passportJwt.ExtractJwt

module.exports = function (config) {
passport.use(new JwtStrategy({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.jwt.secret
}, async (jwtPayload, done) => {
try {
const user = await UserService.getUserDeserializedById(jwtPayload.id)
if (user) {
return done(null, user)
} else {
return done(null, false, {
message: 'Incorrect token'
})
}
} catch (err) {
return done(err)
async function verifyCallback(jwtPayload, done) {
try {
const user = await UserService.getUserDeserializedById(jwtPayload.id)
if (user) {
return done(null, user)
} else {
return done(null, false)
}
}))
} catch (err) {
return done(err)
}
}

module.exports = function (config) {
const jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken()
const secretOrKey = config.jwt.secret

const strategy = new JwtStrategy({jwtFromRequest, secretOrKey}, verifyCallback)

passport.use(strategy)
}

0 comments on commit 3e2644f

Please sign in to comment.