node.js ValidationError: "expiresInMinutes" 是不允许的 NodeJs JsonWebToken

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37629475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:16:52  来源:igfitidea点击:

ValidationError: "expiresInMinutes" is not allowed NodeJs JsonWebToken

node.jsjwt

提问by Abdul Rehman Sayed

I am using NodeJs with JsonWebtoken Module.

我正在将 NodeJs 与 JsonWebtoken 模块一起使用。

I am facing this error when calling sign method of json web token

我在调用 json web token 的 sign 方法时遇到这个错误

ValidationError: "expiresInMinutes" is not allowed

验证错误:“expiresInMinutes”是不允许的

var jwt = require('jsonwebtoken');

exports.authenticate = function(req, res, next) {
    var user = {"Name":"Abdul"} //static data for test purpose.

    var token = jwt.sign(user, req.app.get('jwtTokenSecret'), {
          expiresInMinutes: 1440 // expires in 24 hours
        });

        // return the information including token as JSON
        res.json({
          success: true,
          message: 'Enjoy your token!',
          token: token
        });

}

回答by Abdul Rehman Sayed

Ok I found that from https://www.npmjs.com/package/jsonwebtoken

好的,我从https://www.npmjs.com/package/jsonwebtoken发现

You have to call expiresInrather than expiresInMinutes.

您必须调用expiresIn而不是expiresInMinutes

 var token = jwt.sign(user, req.app.get('jwtTokenSecret'), {
           expiresIn : 60*60*24
         });

Here the value of expiresInis measured in seconds rather than minutes, so the value has to be put in properly.

这里的值expiresIn是以秒而不是分钟来衡量的,因此必须正确输入该值。

回答by kaxi1993

expiresInMinuteswas deprecated, you should use expiresIn: '1440m'for instance

expiresInMinutes已弃用,您应该使用expiresIn: '1440m'例如

回答by edelans

expiresInMinutesis deprecated, use expiresIninstead. From the docs:

expiresInMinutes已弃用,请expiresIn改用。从文档

expiresIn: expressed in seconds or a string describing a time span rauchg/ms. Eg: 60, "2 days", "10h", "7d"

expiresIn: 以秒或描述时间跨度的字符串表示rauchg/ms。例如:60、“2 天”、“10 小时”、“7 天”