'use strict';

//////////////////////////////
// Requires
//////////////////////////////
const express = require('express');
const config = require('config');
const nunjucks = require('nunjucks');
const path = require('path');
const session = require('express-session');

const oauth = require('./lib/oauth')();

//////////////////////////////
// App Variables
//////////////////////////////
const app = express();

//////////////////////////////
// Configuration
//////////////////////////////
app.set('views', path.join(__dirname, 'views'));
nunjucks.configure(['views', 'templates'], {
  'autoescape': true,
  'express': app,
});
app.set('view engine', 'html');

// Session
if (config.cookies.secure) {
  app.set('trust proxy', 1);
}
app.use(session({
  secret: 'babka',
  resave: false,
  saveUninitialized: true,
  cookie: {
    secure: config.cookies.secure,
  },
}));

app.use((req, res, next) => {
  let auth = false;
  let host = `${config.github.api.protocol}://`;

  if (req.session.token) {
    auth = true;
  }

  // Host
  if (config.github.api.host === 'api.github.com') {
    host += 'github.com';
  }
  else {
    host += config.github.api.host;
  }


  req.session.authenticated = auth;
  res.locals.authenticated = auth;

  res.locals.host = host;
  next();
});

//////////////////////////////
// Routing
//////////////////////////////
app.get('/', (req, res) => {
  res.render('index.html');
});

// OAuth
app.get('/login', (req, res) => {
  return oauth.login(req, res);
});

app.get('/callback', (req, res) => {
  return oauth.callback(req, res);
});

oauth.on('error', function (err, token, res, tokenRes, req) {
  req.flash('error message', err);
  res.redirect('/');
});

oauth.on('token', function (token, res, tokenRes, req) {
  req.flash('token', token);
  res.redirect('/');
});

//////////////////////////////
// Start the server
//
// Ignored from code coverage as it's not possible to actually write a test for
// Starts the actual when this file is called directly
//////////////////////////////
/* istanbul ignore if  */
if (!module.parent) {
  app.listen(config.server.port, () => {
    console.log(`Server starting on ${config.server.host}:${config.server.port}`);
  });
}

module.exports = app;

@@ -304,16 +304,61 @@
 uth')();
+%0Aconst repos = require('./lib/github/repos');
 %0A%0A//////
@@ -629,17 +629,16 @@
 '%5D, %7B%0A  
-'
 autoesca
@@ -639,17 +639,16 @@
 toescape
-'
 : true,%0A
@@ -649,25 +649,23 @@
 true,%0A  
-'
 express
-'
 : app,%0A%7D
@@ -1443,16 +1443,157 @@
 ex.html'
+, %7B%0A    token: req.session.token,%0A  %7D);%0A%7D);%0A%0Aapp.get('/repos', (req, res) =%3E %7B%0A  res.render('repos.html', %7B%0A    repos: req.session.repos,%0A  %7D
 );%0A%7D);%0A%0A
@@ -1937,33 +1937,138 @@
   re
-q.flash('
+turn repos(token.access_token).then(allRepos =%3E %7B%0A    req.session.repos = allRepos;%0A    req.session.
 token
-',
+ =
  token
-);%0A
+.access_token;%0A  
   re
@@ -2080,24 +2080,30 @@
 irect('/');%0A
+  %7D);%0A
 %7D);%0A%0A///////

Add Repos and Tokens