{"size": 1365, "ext": "js", "max_stars_repo_path": "server/middlewares/auth.js", "max_stars_repo_name": "AnechaS/backend-chatbot-on-chatfuel", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/middlewares/auth.js", "max_issues_repo_name": "AnechaS/backend-chatbot-on-chatfuel", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/middlewares/auth.js", "max_forks_repo_name": "AnechaS/backend-chatbot-on-chatfuel", "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": 28.4375, "max_line_length": 77, "alphanum_fraction": 0.6271062271}
const createError = require('http-errors');
const httpStatus = require('http-status');
const moment = require('moment');
const Session = require('../models/session.model');

const handleAuthorize = async (req, res, next /* , roles */) => {
  try {
    const sessionToken = req.get('X-Session-Token');

    // check property authorization in headers not provide
    if (!sessionToken) {
      return next(createError(httpStatus.UNAUTHORIZED));
    }

    const session = await Session.findOne({ sessionToken }).populate('user');

    // check session token is exists database
    if (!session) {
      return next(createError(httpStatus.UNAUTHORIZED));
    }

    // check session token is expires
    if (moment(session.expiresAt).isBefore()) {
      return next(
        createError(httpStatus.UNAUTHORIZED, 'Session token expired.')
      );
    }

    // TODO: Promission Role admin, readWrite, read
    // if (roles.length) {
    //   if (!roles.includes(user.role)) {
    //     return next(
    //       createError(httpStatus.FORBIDDEN, 'Session token expired.')
    //     );
    //   }
    // }

    req.user = Object.assign({}, session.user.transform(), { sessionToken });
    return next();
  } catch (error) {
    return next(error);
  }
};

module.exports = function (...roles) {
  return (req, res, next) => handleAuthorize(req, res, next, roles);
};
