Skip to content
Snippets Groups Projects
Commit 9adc7e56 authored by Lucas Gabriel Lima's avatar Lucas Gabriel Lima
Browse files

add function to retrieve jwt from requests

parent f4f892cf
No related branches found
No related tags found
1 merge request!20Auth
Pipeline #
const JwtStrategy = require('passport-jwt').Strategy; const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const libs = `${process.cwd()}/libs`; const libs = `${process.cwd()}/libs`;
const config = require(`${libs}/config`); const config = require(`${libs}/config`);
const User = ('../models/user'); const User = ('../models/user');
module.exports = function(passport){ module.exports = function(passport){
var opts = {}; var opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
opts.secretOrKey = config.get('mongodb:secret'); opts.secretOrKey = config.get('mongodb:secret');
passport.use(new JwtStrategy(opts, function(jwt_payload, done){ passport.use(new JwtStrategy(opts, function(jwt_payload, done){
User.find({id: jwt_payload.id}, function(err, user){ User.find({id: jwt_payload.id}, function(err, user){
...@@ -12,13 +14,13 @@ module.exports = function(passport){ ...@@ -12,13 +14,13 @@ module.exports = function(passport){
return done(err); return done(err);
} }
if (!user) { if (!user) {
return done(null, false, {message: 'Unknown user'}); return done(null, false, {message: 'Unknown user'});
} }
if (!user.comparePassword(password)) { if (!user.comparePassword(password)) {
return done(null, false, {message: 'Invalid password'}); return done(null, false, {message: 'Invalid password'});
} }
return done(null, user); return done(null, user);
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment