diff options
Diffstat (limited to 'server/config')
-rw-r--r-- | server/config/passport.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/server/config/passport.js b/server/config/passport.js new file mode 100644 index 0000000..02ad4f6 --- /dev/null +++ b/server/config/passport.js @@ -0,0 +1,29 @@ +const passport = require('passport'); +const jwtStrategy = require('passport-jwt').Strategy; +const extractJwt = require('passport-jwt').ExtractJwt; + +const {masterKey} = require("../config/env"); +const User = require('../models/User'); + +const options = { + jwtFromRequest: extractJwt.fromAuthHeaderAsBearerToken(), + secretOrKey: masterKey +}; + +const jwtStrategyCallback = (jwtPayload, done) => { + // Try to find user + User.findOne({_id: jwtPayload.sub}, (err, user) => { + // Database error + if (err) + return done(err, false); + + // No user found + if (!user) + return done(null, false); + + // Successful login + return done(null, user); + }) +}; + +passport.use(new jwtStrategy(options, jwtStrategyCallback)); |