{"size": 698, "ext": "js", "max_stars_repo_path": "app/middlewares/validators/authorizationValidator.js", "max_stars_repo_name": "wolox-training/pj-express-js", "max_stars_repo_stars_event_min_datetime": "2020-01-07T16:39:05.000Z", "max_stars_repo_stars_event_max_datetime": "2020-02-17T14:39:16.000Z", "max_issues_repo_path": "app/middlewares/validators/authorizationValidator.js", "max_issues_repo_name": "wolox-training/pj-express-js", "max_issues_repo_issues_event_min_datetime": "2020-06-02T18:10:01.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-02T18:10:01.000Z", "max_forks_repo_path": "app/middlewares/validators/authorizationValidator.js", "max_forks_repo_name": "wolox-training/pj-express-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 25.8518518519, "max_line_length": 66, "alphanum_fraction": 0.5959885387}
const jwt = require('../../services/jwt');
const errors = require('../../errors');
const usersService = require('../../services/users');

exports.validate = (req, res, next) => {
  try {
    const token = jwt.decode(req.headers.authorization);
    req.userToken = token;
  } catch (err) {
    next(errors.authenticationError(err));
    return;
  }
  usersService
    .findUserByMail(req.userToken.mail)
    .then(user => {
      if (user.tokenEmitDate / 1000 <= req.userToken.iat) {
        req.user = user.dataValues;
        next();
        return;
      }
      next(errors.authenticationError('User Token has expired.'));
    })
    .catch(err => {
      next(errors.notFound(err));
    });
};
