-
Notifications
You must be signed in to change notification settings - Fork 4
/
passport.js
39 lines (35 loc) · 1.22 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
32
33
34
35
36
37
38
39
const passport = require("passport");
const pool = require("./database");
const config = require("./config/config");
const { parseResultSet } = require("./helpers");
const JwtStrategy = require("passport-jwt").Strategy,
ExtractJwt = require("passport-jwt").ExtractJwt;
var opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = config.authentication.jwtSecret;
passport.use(
new JwtStrategy(opts, async function(jwtPayload, done) {
try {
//console.log("jwtPayload is ", jwtPayload);
let sql = "SELECT user_id FROM user WHERE user_id =?";
pool.query(sql, [jwtPayload.id]).then(result => {
let data = parseResultSet(result);
let user = { ...data[0] };
//console.log("user object from query is ", user);
if (!user) {
return done(new Error(), false);
} else if (jwtPayload.id === user.id) {
//console.log("user match");
return done(null, user);
} else {
console.log("user not match");
return done(new Error(), false);
}
});
} catch (err) {
console.log("error in passport.use()", err);
return done(new Error(), false);
}
})
);
module.exports = null;