guard.js 563 B

123456789101112131415161718192021
  1. const passport = require("passport");
  2. require("../config/passport");
  3. const chalk = require("chalk");
  4. const guard = (req, res, next) => {
  5. passport.authenticate("jwt", { session: false }, (err, user) => {
  6. const token = req.get("Authorization")?.split(" ")[1];
  7. if (!user || err || token !== user.token) {
  8. return res.status(403).json({
  9. status: "error",
  10. code: 403,
  11. data: "Forbidden",
  12. message: "Access is denied",
  13. });
  14. }
  15. req.user = user;
  16. return next();
  17. })(req, res, next);
  18. };
  19. module.exports = guard;