admin管理员组文章数量:1406160
I am working on an Express.js application where I use JWT for authentication. My authentication and verification setup works fine in Postman, but when I try to make requests from my frontend, I get the following error in my backend console:
JsonWebTokenError: invalid signature
at C:\Users\lanci\node_modules\jsonwebtoken\verify.js:133:19
at getSecret (C:\Users\lanci\node_modules\jsonwebtoken\verify.js:90:14)
at module.exports [as verify] (C:\Users\lanci\node_modules\jsonwebtoken\verify.js:94:10)
at authMiddleware (file:///C:/Users/lanci/Desktop/Application_MyTicket/Admin_Dashboard/React-CRUD-Operation/anisateur_backend/middleware/auth.js:21:25)
Here is my JWT token generation code in authController.js:
import jwt from "jsonwebtoken";
const generateToken = (user) => {
return jwt.sign(
{ id: user.id, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: "1h" }
);
};
// Example usage in login
const login = async (req, res) => {
// User authentication logic
const token = generateToken(user);
res.json({ token });
};
I have verified that:
process.env.JWT_SECRET is correctly loaded in both server.js and auth.js. The token works fine in Postman, but fails when making requests from the frontend. When pasting the token into jwt.io, the signature shows as invalid. Restarting the server and regenerating a new token does not solve the issue. What could be causing this error? How can I ensure the token verification works properly in both Postman and the frontend?
本文标签: nodejsJWT Token Verification Fails with 39invalid signature39 in Expressjs MiddlewareStack Overflow
版权声明:本文标题:node.js - JWT Token Verification Fails with 'invalid signature' in Express.js Middleware - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744976518a2635551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论