护照 JWT:req.user 始终未授权
Passport JWT : req.user is always Unauthorized
我在堆栈溢出上搜索过类似的问题,但其中任何一个都不适合我,所以我再次问这个问题。
我正在使用 passport-jwt 和 express
。
I want to get logged in user id , to store it in other model. but every time I get 401 Unauthorized
这里是helpers/passport.js
const options = {};
options.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
options.secretOrKey = configuration.secret_key.JWTsecretkey;
module.exports = passport => {
passport.use(
new JwtStrategy(options, (jwt_payload, done) => {
console.log('jwt_payload ==',jwt_payload);
User.findById(jwt_payload.id)
.then(user => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => {
console.log(err);
return done(err, false);
});
})
);
};
这里是server.js
global.express = require('express');
global.app = express();
global.passport = require('passport');
global.jwt = require('jsonwebtoken');
global.JwtStrategy = require('passport-jwt').Strategy;
global.ExtractJwt = require('passport-jwt').ExtractJwt;
global.requireTree = require('require-tree')
global.controllers = requireTree(rootdir+'/controllers')
global.routes=requireTree(rootdir+'/routes')
global.helpers = requireTree(rootdir + '/helpers')
app.use(passport.initialize())
require(rootdir+'/helpers/passport')(passport)
app.use('/api/auth',routes.api.auth);
app.listen(8888)
routes/api/auth.js
const router=express.Router();
router.get('/user', passport.authenticate('jwt', { session: false }),controllers.auth.user);
module.exports =router
controllers/auth/user.js
module.exports=(req,res)=>{
res.json({
user:req.user
});
}
当我尝试使用邮递员但没有工作时。
我也试过:ExtractJwt.fromAuthHeaderWithScheme('jwt')
因为它在某些问题中得到了支持,但它不起作用。
我正在使用
"passport": "^0.4.0" ,
"passport-jwt": "^4.0.0" ,
"jsonwebtoken": "^8.3.0"
请帮我解决这个问题。
I have solved above errors by replacing
User.findById(jwt_payload.id)
with
User.findById(jwt_payload._id)
此处更新passport.js
const options = {};
options.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
options.secretOrKey = configuration.secret_key.JWTsecretkey;
module.exports = passport => {
passport.use(
new JwtStrategy(options, (jwt_payload, done) => {
console.log('jwt_payload ==',jwt_payload);
User.findById(jwt_payload._id)
.then(user => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => {
console.log(err);
return done(err, false);
});
})
);
};
我在堆栈溢出上搜索过类似的问题,但其中任何一个都不适合我,所以我再次问这个问题。
我正在使用 passport-jwt 和 express
。
I want to get logged in user id , to store it in other model. but every time I get 401 Unauthorized
这里是helpers/passport.js
const options = {};
options.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
options.secretOrKey = configuration.secret_key.JWTsecretkey;
module.exports = passport => {
passport.use(
new JwtStrategy(options, (jwt_payload, done) => {
console.log('jwt_payload ==',jwt_payload);
User.findById(jwt_payload.id)
.then(user => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => {
console.log(err);
return done(err, false);
});
})
);
};
这里是server.js
global.express = require('express');
global.app = express();
global.passport = require('passport');
global.jwt = require('jsonwebtoken');
global.JwtStrategy = require('passport-jwt').Strategy;
global.ExtractJwt = require('passport-jwt').ExtractJwt;
global.requireTree = require('require-tree')
global.controllers = requireTree(rootdir+'/controllers')
global.routes=requireTree(rootdir+'/routes')
global.helpers = requireTree(rootdir + '/helpers')
app.use(passport.initialize())
require(rootdir+'/helpers/passport')(passport)
app.use('/api/auth',routes.api.auth);
app.listen(8888)
routes/api/auth.js
const router=express.Router();
router.get('/user', passport.authenticate('jwt', { session: false }),controllers.auth.user);
module.exports =router
controllers/auth/user.js
module.exports=(req,res)=>{
res.json({
user:req.user
});
}
当我尝试使用邮递员但没有工作时。
我也试过:ExtractJwt.fromAuthHeaderWithScheme('jwt')
因为它在某些问题中得到了支持,但它不起作用。
我正在使用
"passport": "^0.4.0" , "passport-jwt": "^4.0.0" ,
"jsonwebtoken": "^8.3.0"
请帮我解决这个问题。
I have solved above errors by replacing
User.findById(jwt_payload.id)
with
User.findById(jwt_payload._id)
此处更新passport.js
const options = {};
options.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
options.secretOrKey = configuration.secret_key.JWTsecretkey;
module.exports = passport => {
passport.use(
new JwtStrategy(options, (jwt_payload, done) => {
console.log('jwt_payload ==',jwt_payload);
User.findById(jwt_payload._id)
.then(user => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => {
console.log(err);
return done(err, false);
});
})
);
};