apiRoutes.post('/authenticate', function(req, res) {
User.findOne({
name: req.body.name
}, function(err, user) {
if (err) throw err;
if (!user) {
res.send({success: false, msg: 'Authentication failed. User not found.'});
} else {
// check if password matches
user.comparePassword(req.body.password, function (err, isMatch) {
if (isMatch && !err) {
// if user is found and password is right create a token
var expires=moment().add(1,'days').valueOf();
var token = jwt.encode(user, config.secret,{
exp: expires});
// return the information including token as JSON
res.json({success: true, token: 'JWT ' + token});
} else {
res.send({success: false, msg: 'Authentication failed. Wrong password.'});
}
});
}
});
});It提示错误: /home/oracle/node/ang_backend1/node_modules/jwt-simple/lib/jwt.js:130抛出新错误(‘算法不受支持’);^
错误:位于/home/oracle/node/ang_backend1/app.js:198:27 at /home/oracle/node/ang_backend1/app/models/user.js:48:9 at /home/oracle/node/ang_backend1/node_modules/bcryptjs/dist/bcrypt.js:261:17 at /home/oracle/node/ang_backend1/node_modules/bcryptjs/dist/bcrypt.js:1198:21的Object.jwt_encode as encode不支持算法在processImmediate as _immediateCallback的Immediate.next as _onImmediate
发布于 2017-01-15 23:54:14
我认为你没有得到令牌,你应该这样做:
var expires=moment().add(1,'days').valueOf();
var token = jwt.encode(user, config.secret,{
exp: expires}),app.get('jwtTokenSecret'));发布于 2017-08-24 18:00:51
您的安全路由应定义为:
router.get('/enums', passport.authenticate('jwt', {session: false}), async (req, res) => { ...const passport = require('passport')在哪里。
为了向令牌添加过期时间,您必须设置两个参数(以秒为单位):exp和nbf,如下所示:
let expires = (Date.now() / 1000) + 60 * 30
let nbf = Date.now() / 1000
let token = await jwt.encode({nbf: nbf, exp: expires, id: user_id}, jwtSecret).where const jwt = require('jwt-simple')
https://stackoverflow.com/questions/41662873
复制相似问题