【发布时间】:2022-01-17 14:48:25
【问题描述】:
在 user.js 文件中,我使用此代码在此处创建了令牌
if (user && bcrypt.compareSync(req.body.password, user.passwordHash)) {
const token = jwt.sign(
{
userId: user.id,
isAdmin: user.isAdmin,
},
process.env.SECRET,
{
expiresIn: "50d", // >> on day
}
);
令牌工作,一切正常,但我想在其他地方使用令牌,例如,在 cupon.js 文件中
router.post("/cupon", async (req, res) => {
...
const token = req.header("authorization").substring(7);
...
我用过这个
const token = req.header("authorization").substring(7);
从header中获取token,有没有更好的获取token的方法?
【问题讨论】: