mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 11:49:19 +00:00
Migrate from moment to date-fns and some changes
This commit is contained in:
23
backend/src/middleware/is-auth.js
Normal file
23
backend/src/middleware/is-auth.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
module.exports = (req, res, next) => {
|
||||
let decodedToken;
|
||||
try {
|
||||
const [, token] = req.get("Authorization").split(" ");
|
||||
decodedToken = jwt.verify(token, "mysecret");
|
||||
// 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;
|
||||
} catch (err) {
|
||||
err.statusCode = 401;
|
||||
err.message = "invalidToken";
|
||||
next(err);
|
||||
}
|
||||
|
||||
if (!decodedToken) {
|
||||
const error = new Error("Falha na autenticação");
|
||||
error.statusCode = 401;
|
||||
next(error);
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
Reference in New Issue
Block a user