From 4c83aac57e157ce67b30e645384c632360ed8ea9 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Lima <lgl15@inf.ufpr.br> Date: Fri, 16 Sep 2016 10:57:33 -0300 Subject: [PATCH] add passport config --- src/libs/middlewares/passport.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/libs/middlewares/passport.js diff --git a/src/libs/middlewares/passport.js b/src/libs/middlewares/passport.js new file mode 100644 index 00000000..df3e56e0 --- /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); + }); + })); +}; -- GitLab