-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport-config.js
33 lines (26 loc) · 905 Bytes
/
passport-config.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
32
33
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const { ExtractJwt } = require('passport-jwt');
const { SECRET_KEY } = require('./config');
const User = require('./models/User');
const opts = {
// This looks for header on all client requests:
// Authorization: JWT [jwt-string]
jwtFromRequest: ExtractJwt.fromAuthHeader(),
secretOrKey: SECRET_KEY,
};
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
// this callback only runs if JWT token has successfully verified
// against server secret signature
const user = User.findOneById(jwt_payload.sub);
const foundToken = user.tokens.find(t => t.iat === jwt_payload.iat);
if (!foundToken) {
return done(null, false, 'Token does not exist');
}
if (user) {
done(null, user);
} else {
done(null, false, 'User does not exist');
}
}));
module.exports = passport;