diff --git a/src/libs/middlewares/passport.js b/src/libs/middlewares/passport.js new file mode 100644 index 0000000000000000000000000000000000000000..df3e56e022308aa877cf499d2689e32c1de950a8 --- /dev/null +++ b/src/libs/middlewares/passport.js @@ -0,0 +1,26 @@ +const JwtStrategy = require('passport-jwt').Strategy; +const libs = `${process.cwd()}/libs`; +const config = require(`${libs}/config`); +const User = ('../models/user'); + +module.exports = function(passport){ + var opts = {}; + opts.secretOrKey = config.get('mongodb:secret'); + passport.use(new JwtStrategy(opts, function(jwt_payload, done){ + User.find({id: jwt_payload.id}, function(err, user){ + if (err) { + return done(err); + } + + if (!user) { + return done(null, false, {message: 'Unknown user'}); + } + + if (!user.comparePassword(password)) { + return done(null, false, {message: 'Invalid password'}); + } + + return done(null, user); + }); + })); +};