【发布时间】:2018-07-06 16:39:55
【问题描述】:
我正在使用 Vue 构建我的第一个 SPA 项目。
我决定使用 NodeJS 作为后端,但是,我很头疼使用 JsonWebToken 构建登录功能。
我写了一些代码来看看 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
下面是我的 server.js 的代码
这是导入东西的代码。
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 }));
这是用于发布 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});
});
这是检查 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 });
}
});
【问题讨论】:
-
返回什么:
const token = req.body.jwt;IE,console.log(token)给出了什么? -
应该是收到的JWT token。后者将显示它收到的令牌。
标签: javascript node.js express login jwt