简体   繁体   English

ValidationError:“expiresInMinutes”不允许NodeJs JsonWebToken

[英]ValidationError: “expiresInMinutes” is not allowed NodeJs JsonWebToken

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 ValidationError:不允许使用“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
        });

}

Ok I found that from https://www.npmjs.com/package/jsonwebtoken 好的,我从https://www.npmjs.com/package/jsonwebtoken找到了

You have to call expiresIn rather than expiresInMinutes . 你必须调用expiresIn而不是expiresInMinutes

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

Here the value of expiresIn is measured in seconds rather than minutes, so the value has to be put in properly. 这里expiresIn的值是以秒为单位而不是分钟来衡量的,因此必须正确地输入值。

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

expiresInMinutes is deprecated, use expiresIn instead. expiresInMinutes已过时,使用expiresIn代替。 From the docs : 来自文档

expiresIn : expressed in seconds or a string describing a time span rauchg/ms . expiresIn :以秒表示或描述时间跨度rauchg / ms的字符串。 Eg: 60, "2 days", "10h", "7d" 例如:60,“2天”,“10h”,“7d”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM