javascript - TypeError : jwt. 符号不是函数

标签 javascript node.js jwt

我包括 express 等包括:

var expressJwt = require('express-jwt'); //https://npmjs.org/package/express-jwt
var secret = 'this is the secret secret secret 12356';
var jwt = require('jsonwebtoken');  //https://npmjs.org/package/node-jsonwebtoken

然后定义我的续集模型和尾声路线并将其放置在这里:

app.post('/authenticate', function (req, res) {
  //TODO validate req.body.username and req.body.password
  //if is invalid, return 401
  if (!(req.body.username === 'john.doe' && req.body.password === 'foobar')) {
    res.status(401).send('Wrong user or password');
    return;
  }

  var profile = {
    first_name: 'John',
    last_name: 'Doe',
    email: 'john@doe.com',
    id: 123
  };

  // We are sending the profile inside the token
  var token = jwt.sign(profile, secret, { expiresInMinutes: 60*5 });

  res.json({ token: token });
});

当我在表单中输入 john.doe 和 foobar 时,控制台告诉我 jwt.sign 不是一个函数,即使在 npm 安装之后也是如此。

最佳答案

jsonwebtoken 仅用于验证/解码 express.js 上的 jwts 请求

如果您需要签署请求,您需要使用 node-jsonwebtoken:

https://github.com/auth0/node-jsonwebtoken

生长激素问题:

https://github.com/auth0/express-jwt/issues/48

这是一篇关于您正在尝试做的事情的精彩博文:

https://matoski.com/article/jwt-express-node-mongoose/

关于javascript - TypeError : jwt. 符号不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299386/

相关文章:

带有公钥/私钥的java-jwt

authentication - 如何在 API 后端从 AWS Cognito 验证 JWT?

javascript - 如何使用 Fetch API 像 Axios 中那样 POST 数据和参数

javascript - JS toLowerCase() 不起作用

javascript - 如何在不知道mongodb中的父键的情况下查找特定的嵌套对象

Node.js 10 HTTPS 服务器拒绝连接

javascript - Node.JS 单套接字,多个事件(上传)

Angularjs使用Facebook认证策略不拦截nodejs服务器提供的token

javascript - 如果不使用 XMLHttpRequest,我们如何使用 JavaScript 跨域获取网页?有没有可以做到这一点的插件?

java - 使用 JAVA 更快地验证 Cognito JWT token 的方法