我正在尝试使用express-jwt和我的app.js文件中的中间件函数来创建登录功能。但是每当我尝试使用postman发送get请求时,它发送请求的时间是无限的,并且从不返回任何错误或成功消息。我使用dynamoDB作为数据库。
这是我的Login.js文件
const AWS = require("aws-sdk");
const express = require("express");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
require("dotenv").config();
AWS.config.update({ region: "us-east-2" });
const docClient = new AWS.DynamoDB.DocumentClient();
const router = express.Router();
router.post("/login", (req, res) => {
user_type = "customer";
const email = req.body.email;
docClient.get(
{
TableName: "users",
Key: {
user_type,
email,
},
},
(err, data) => {
if (err) {
res.send("Invalid username or password");
} else {
if (data && bcrypt.compareSync(req.body.password, data.Item.password)) {
const token = jwt.sign(
{
email: data.Item.email,
},
process.env.SECRET,
{ expiresIn: "1d" }
);
res.status(200).send({ user: data.Item.email, token: token });
} else {
res.status(400).send("Password is wrong");
}
}
}
);
});
module.exports = router;
这是我的jwt.js文件:
const expressJwt = require("express-jwt");
require("dotenv").config();
function authJwt() {
const secret = process.env.SECRET;
return expressJwt({
secret,
algorithms: ["HS256"],
});
}
module.exports = authJwt;
我试着在我的app.js文件中像这样使用expressJwt:
app.use(authJwt); //If I'm not using this, then the code works fine without API protection
有人能告诉我我的代码出了什么问题吗?感谢你方的任何帮助。
发布于 2021-07-21 04:40:13
从你的jwt.js中删除函数,它应该看起来像这样
const expressJwt = require('express-jwt');
const secret = process.env.secret
const authJwt = expressJwt({
secret,
algorithms:['HS256']
})
module.exports = authJwt;https://stackoverflow.com/questions/66890288
复制相似问题