{"size": 1003, "ext": "js", "max_stars_repo_path": "server/services/forceAuth.js", "max_stars_repo_name": "nprapps/dailygraphics-next", "max_stars_repo_stars_event_min_datetime": "2018-12-31T16:05:51.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-11T06:49:06.000Z", "max_issues_repo_path": "server/services/forceAuth.js", "max_issues_repo_name": "nprapps/dailygraphics-next", "max_issues_repo_issues_event_min_datetime": "2019-01-04T23:08:48.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-28T18:33:51.000Z", "max_forks_repo_path": "server/services/forceAuth.js", "max_forks_repo_name": "nprapps/dailygraphics-next", "max_forks_repo_forks_event_min_datetime": "2018-12-31T18:18:20.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-15T01:14:57.000Z"}
{"max_stars_count": 38, "max_issues_count": 46, "max_forks_count": 17, "avg_line_length": 29.5, "max_line_length": 78, "alphanum_fraction": 0.6111665005}
var { testConnection } = require("../../lib/sheetOps");
var https = require("https");

var lastCheck = null;
var checkInterval = 5 * 60 * 1000; // five minute pause on connection tests

module.exports = function(app) {
  var check = async function(request, response, next) {
    var app = request.app;
    var config = app.get("config");
    var now = Date.now();
    if (lastCheck && now - lastCheck < checkInterval) {
      request.user = config.user;
      return next();
    }
    lastCheck = now;
    try {
      // this will throw if user isn't logged in
      config.user = request.user = await testConnection();
      next();
    } catch (err) {
      console.log(`Unable to authorize Google connection ("${err.message}")`);
      // not connected, reroute to auth
      console.log("Redirecting to authorization page");
      response.status(302);
      response.set("Location", "/authorize");
      response.send();
    }
  };

  app.use("/$", check);
  app.use("/graphic/:slug/$", check);
};
