首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将过期日期添加到jwt-simple token

如何将过期日期添加到jwt-simple token
EN

Stack Overflow用户
提问于 2017-01-15 23:33:07
回答 2查看 2.3K关注 0票数 0
代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2017-01-15 23:54:14

我认为你没有得到令牌,你应该这样做:

代码语言:javascript
复制
var expires=moment().add(1,'days').valueOf();
          var token = jwt.encode(user, config.secret,{
          exp: expires}),app.get('jwtTokenSecret'));
票数 0
EN

Stack Overflow用户

发布于 2017-08-24 18:00:51

您的安全路由应定义为:

代码语言:javascript
复制
router.get('/enums', passport.authenticate('jwt', {session: false}), async (req, res) => { ...

const passport = require('passport')在哪里。

为了向令牌添加过期时间,您必须设置两个参数(以秒为单位):expnbf,如下所示:

代码语言:javascript
复制
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')

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41662873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档