-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
31 lines (27 loc) · 1 KB
/
passport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const User = require('./sequelizeModels').User;
const config = require('./core/default.json');
const passport = require('passport')
const passportJwt = require('passport-jwt')
const { Strategy, ExtractJwt } = passportJwt
module.exports = function() {
const params = {
secretOrKey: config.security.jwtPrivateKey,
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
}
const strategy = new Strategy(params, async (payload, done) => {
try {
const user = await User.findOne( { where: { id: payload.user.id } } );
if (user)
return done(null, user);
else
return done(null, false);
} catch (err) {
return done(err, false);
}
});
passport.use(strategy);
return {
initialize: function() { return passport.initialize(); },
authenticate: function() { return passport.authenticate('jwt', { session: false }); }
};
};