mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 21:29:25 +00:00
impromente: better jwt errors handler
This commit is contained in:
@@ -1,17 +1,22 @@
|
|||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
|
|
||||||
const authConfig = require("../config/auth");
|
const authConfig = require("../config/auth");
|
||||||
|
|
||||||
module.exports = (req, res, next) => {
|
module.exports = async (req, res, next) => {
|
||||||
let decodedToken;
|
const authHeader = req.headers.authorization;
|
||||||
|
|
||||||
const [, token] = req.get("Authorization").split(" ");
|
if (!authHeader) {
|
||||||
decodedToken = jwt.verify(token, authConfig.secret);
|
return res.status(401).json({ error: "Token not provided" });
|
||||||
// todo >> find user in DB and store in req.user to use latter, or throw an error if user not exists anymore
|
|
||||||
req.userId = decodedToken.userId;
|
|
||||||
|
|
||||||
if (!decodedToken) {
|
|
||||||
return res.status(401).json({ message: "Unauthorized" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
const [, token] = authHeader.split(" ");
|
||||||
|
|
||||||
|
jwt.verify(token, authConfig.secret, (error, result) => {
|
||||||
|
if (error) {
|
||||||
|
return res.status(401).json({ error: "Invalid token" });
|
||||||
|
}
|
||||||
|
req.userId = token.userId;
|
||||||
|
// todo >> find user in DB and store in req.user to use latter, or throw an error if user not exists anymore
|
||||||
|
next();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user