javascript 使用 JsonWebToken 时遇到问题;JsonWebToken 错误:必须提供 JWT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48476625/
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-10-29 08:04:01  来源:igfitidea点击:

Having trouble with JsonWebToken; JsonWebToken Error: JWT must be provided

javascriptnode.jsexpressloginjwt

提问by HelloWorld

I'm building my first SPA project with Vue.

我正在用 Vue 构建我的第一个 SPA 项目。

I decided to go with NodeJS for the back-end, however, I'm having a headache building the login function with the JsonWebToken.

我决定使用 NodeJS 作为后端,但是,我在使用 JsonWebToken 构建登录功能时遇到了麻烦。

I had wrote some codes to see how JWT works and when I tried to see how JWT gets verified, server gave me an error.

我写了一些代码来了解 JWT 是如何工作的,当我试图了解 JWT 如何被验证时,服务器给了我一个错误。

JsonWebTokenError: jwt must be provided
at Object.module.exports [as verify] (c:\dir\node_modules\jsonwebtoken\verify.js:39:17)
at c:\projects\practice\demo\back\server.js:34:17

Below is the code for my server.js

下面是我的 server.js 的代码

This is the code for importing the stuff.

这是导入东西的代码。

const express = require('express');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const api = express();

api.use(bodyParser.json());
api.use(bodyParser.urlencoded({ extended: true }));

This is for the API for issuing JWT.

这是用于发布 JWT 的 API。

api.post('/secure', function (req, res) {
const token = jwt.sign({ user: {id:1, name:'ME!', role: 'average'} }, 'dsfklgj');
console.log(token);
res.json({jwt: token});
});

This is the API for checking JWT.

这是用于检查 JWT 的 API。

api.post('/check/post', function (req, res) {
const token = req.body.jwt;
const x = jwt.verify(token, 'dsfklgj', function (err, decoded) {
if (err) throw err;
console.log(decoded);
});
if (x != true) {
res.json({ auth: false });
}else {
res.json({ auth: true });
}
});

回答by Mohammad Rajabloo

jwt must be provided

必须提供 jwt

This error happens when the coming token is null or empty.

当即将到来的令牌为空或空时,会发生此错误。

回答by mabc224

It may be you have not defined jwtin specific file or it is null or empty. Therefore you are getting an error. I just test your code and it works for me. It may be that you are not sending jwttoken into post request correctly.

可能是您没有jwt在特定文件中定义或者它为空或空。因此,您收到错误消息。我只是测试你的代码,它对我有用。可能是您没有jwt正确地将令牌发送到 post 请求中。

const express = require('express');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const http = require('http');
const api = express();

api.use(bodyParser.json());
api.use(bodyParser.urlencoded({ extended: true }));

api.post('/secure', function(req, res) {
    const token = jwt.sign({ user: { id: 1, name: 'ME!', role: 'average' } }, 'dsfklgj');
    console.log(token);
    res.json({ jwt: token });
});


api.post('/check/post', function(req, res) {
    const token = req.body.jwt;
    console.log('token: ' + token);
    const x = jwt.verify(token, 'dsfklgj', function(err, decoded) {
        if (err) throw err;
        console.log(decoded);
    });
    console.log(x);
    if (x != true) {
        res.json({ auth: false });
    } else {
        res.json({ auth: true });
    }
});

api.set('port', 3000);
var server = http.createServer(api);
server.listen(api.get('port'), function() {
    console.log("Express server listening on port " + api.get('port'));
});

BTW there is no way to test it like like this const x = jwt.verify(token, 'dsfklgj', function (err, decoded) {. Either write it in Syncway or check condition in asynccallback function. In your case, xwill be undefinedand no guarantee when it will run.

顺便说一句,没有办法像这样测试它const x = jwt.verify(token, 'dsfklgj', function (err, decoded) {。以Sync方式编写或在async回调函数中检查条件。在您的情况下,xundefined不会保证何时运行。