forked from yalla-coop/connect5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
31 lines (28 loc) · 1.03 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 JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");
const Trainer = require("../server/database/models/Trainer");
// const keys = require('../config/keys');
require("env2")("./config/config.env");
// set up options
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = process.env.secretOrKey;
// opts.secretOrKey = keys.secretOrKey;
module.exports = (passport) => {
passport.use(
// use mongoose findById method and pass it the id stemming from the bearer toke auth object named jwt payload
new JwtStrategy(opts, (jwt_payload, done) => {
console.log("JWT", jwt_payload);
Trainer.findById(jwt_payload.id)
.then((trainer) => {
// use done function without error and either user or false
if (trainer) {
return done(null, trainer);
}
return done(null, false);
})
.catch(err => console.log(err));
}),
);
};