const http = require('http');
const bodyParser = require('body-parser');
const session = require('express-session')
const config = require("./config")
const express = require('express');
const path = require('path');
const Trello = require('./trello');
const Promise = require('promise');

Promise.prototype.complete = function(res){
  return this.then(_ => res.json(_), _ => res.status(500).send(_));
};

const port = 3000;

const app = express();
const publicRouter = express.Router();
const secureRouter = express.Router();

publicRouter.get('/authentication-status', (req, res) => {
  const response = req.session.token
    ? {authenticated: true}
    : {key: config.trelloKey, authenticated: false}
  res.json(response);
});

publicRouter.post('/token', (req, res) => {
  req.session.token = req.body.token;
  res.json({authenticated: true});
});

secureRouter.use((req, res, next) => {
  if (req.session.token) {
    next();
  } else {
    res.status(401).send("You are not authorized to access this resource.");
  }
});

secureRouter.get('/me', (req, res) => {
  Trello.me(req.session.token).complete(res);
});

secureRouter.get('/boards', (req, res) => {
  Trello.myBoards(req.session.token).complete(res);
});

secureRouter.get('/boards/:boardId/lists', (req, res) => {
  Trello.lists(req.session.token, req.params.boardId).complete(res);
});

app.use(session(
  {
    cookie: { maxAge: 1000 * 60 * 60 },
    secret: config.sessionSecret,
    resave: false,
    saveUninitialized: false
  }));

app.use(bodyParser.json());
app.use("/api", publicRouter, secureRouter);
app.use("/build", express.static(path.join(__dirname, "../build")));
app.get('/', (req, res) => res.sendFile(path.join(__dirname, "../client/index.html")))

module.exports = {
  start: () => {
    app.listen(port, () => {
      console.log(`Server running at http://127.0.0.1:${port}/`);
    });
  }
};
@@ -324,15 +324,19 @@
 tion
+ 
 (res)
+ 
 %7B%0A
+  
   re
@@ -584,16 +584,18 @@
 s) =%3E %7B%0A
+  
   const 
@@ -627,15 +627,20 @@
 ken%0A
+    
     ? %7B
+ 
 auth
@@ -658,17 +658,23 @@
 true
+ 
 %7D%0A
+    
     : %7B
+ 
 key:
@@ -712,18 +712,21 @@
 d: false
+ 
 %7D%0A
+  
   res.js
@@ -788,16 +788,18 @@
 s) =%3E %7B%0A
+  
   req.se
@@ -828,16 +828,18 @@
 .token;%0A
+  
   res.js
@@ -842,16 +842,17 @@
 s.json(%7B
+ 
 authenti
@@ -862,16 +862,17 @@
 ed: true
+ 
 %7D);%0A%7D);%0A
@@ -911,16 +911,18 @@
 t) =%3E %7B%0A
+  
   if (re
@@ -936,24 +936,28 @@
 on.token) %7B%0A
+    
     next();%0A
@@ -956,16 +956,18 @@
 next();%0A
+  
   %7D else
@@ -969,24 +969,28 @@
  else %7B%0A    
+ 
+   
 res.status(4
@@ -1052,16 +1052,18 @@
 e.%22);%0A  
+ 
+ 
 %7D%0A%7D);%0A%0As
@@ -1093,32 +1093,34 @@
 (req, res) =%3E %7B%0A
+  
   Trello.me(req.
@@ -1196,24 +1196,26 @@
 res) =%3E %7B%0A  
+ 
+ 
 Trello.myBoa
@@ -1316,16 +1316,18 @@
 s) =%3E %7B%0A
+  
   Trello
@@ -1411,22 +1411,28 @@
 sion(%0A  
-%7B%0A
+  %7B%0A   
     
+ 
 cookie: 
@@ -1459,16 +1459,20 @@
 * 60 %7D,%0A
+    
     secr
@@ -1501,16 +1501,20 @@
 et,%0A    
+   
+ 
 resave: 
@@ -1520,16 +1520,20 @@
  false,%0A
+    
     save
@@ -1553,16 +1553,18 @@
 : false%0A
+  
   %7D));%0A%0A
@@ -1812,16 +1812,18 @@
 rts = %7B%0A
+  
   start:
@@ -1827,24 +1827,28 @@
 rt: () =%3E %7B%0A
+    
     app.list
@@ -1860,24 +1860,30 @@
 rt, () =%3E %7B%0A
+      
       consol
@@ -1940,16 +1940,20 @@
 %60);%0A
+    
     %7D);%0A
   %7D%0A
@@ -1948,14 +1948,16 @@
     %7D);%0A
+  
   %7D%0A%7D;

Fix indentation of server.js