mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
22 lines
433 B
JavaScript
22 lines
433 B
JavaScript
const jwt = require("jsonwebtoken");
|
|
|
|
module.exports = (req, res, next) => {
|
|
let decodedToken;
|
|
try {
|
|
const token = req.get("Authorization").split(" ")[1];
|
|
decodedToken = jwt.verify(token, "mysecret");
|
|
} catch (err) {
|
|
err.statusCode = 401;
|
|
next(err);
|
|
}
|
|
|
|
if (!decodedToken) {
|
|
const error = new Error("Falha na autenticação");
|
|
error.statusCode = 401;
|
|
next(error);
|
|
}
|
|
|
|
req.userId = decodedToken.userId;
|
|
next();
|
|
};
|