{"size": 700, "ext": "js", "max_stars_repo_path": "middleware/auth.js", "max_stars_repo_name": "harish32/ticket-master", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "middleware/auth.js", "max_issues_repo_name": "harish32/ticket-master", "max_issues_repo_issues_event_min_datetime": "2021-05-11T03:53:30.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T05:08:09.000Z", "max_forks_repo_path": "middleware/auth.js", "max_forks_repo_name": "harish32/ticket-master", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 26.9230769231, "max_line_length": 63, "alphanum_fraction": 0.5957142857}
const User = require("../models/User");
const jwt = require("jsonwebtoken");
const pick = require("lodash/pick");
const ErrorResponce = require("../utils/ErrorResponce");

exports.protect = async (req, res, next) => {
  try {
    if (!req.cookies.token) {
      return next(new ErrorResponce("you need to login", 401));
    }
    const data = jwt.decode(req.cookies.token);
    if (data) {
      const user = await User.findById(data.id);
      if (user) {
        req.user = user;
        return next();
      }
      return next(new ErrorResponce("you need to login", 401));
    } else {
      return next(new ErrorResponce("you need to login", 401));
    }
  } catch (err) {
    next(err);
  }
};
