Skip to content

Instantly share code, notes, and snippets.

@kianaditya
Created June 22, 2020 15:21
Show Gist options
  • Save kianaditya/466db7fb9befbacb10d255425daf2332 to your computer and use it in GitHub Desktop.
Save kianaditya/466db7fb9befbacb10d255425daf2332 to your computer and use it in GitHub Desktop.
const { Strategy, ExtractJwt } = require('passport-jwt')
const salt = 'thisismysecretstringthelogngerthebetter'
const models = require('./models')
const passport = require('passport')
const options = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), // need to add token to header in tests
secretOrKey: salt
}
module.exports = (passport) => {
passport.use(
new Strategy(options, (payload, done) => {
models.Author.findOne({ where: { name: payload.name } })
.then(user => {
//success, user is found
return done(null, {
id: user.id,
name: user.name
})
})
.catch(error => {
// failure, user is NOT found
console.error(error)
return done(null, false)
})
})
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment