{"size": 478, "ext": "js", "max_stars_repo_path": "middleware/restricted.js", "max_stars_repo_name": "valerievazq/Back-End", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "middleware/restricted.js", "max_issues_repo_name": "valerievazq/Back-End", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "middleware/restricted.js", "max_forks_repo_name": "valerievazq/Back-End", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.7619047619, "max_line_length": 62, "alphanum_fraction": 0.6338912134}
const jwt = require("jsonwebtoken");
const users = require("../users/model");

async function restricted(req, res, next) {
  try {
    const decoded = jwt.verify(
      req.headers.authorization,
      process.env.JWT_SECRET
    );
    const user = await users.getUserById(decoded.subject);
    if (!user) throw new Error();
    req.user = user;

    next();
  } catch (error) {
    res.status(401).json({ message: "you shall not pass!!" });
  }
}

module.exports = restricted;
