/* api.js - All API routes */

const database = require('./database');

const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const Promise = require('bluebird');

const router = express.Router();

//Cookie Setup
const sessionCookie = 'currentSession';
const userCookie = 'currentUser';

//Body Parser Setup
const urlEncoded = bodyParser.urlencoded({ extended: false });

router.post('/login', urlEncoded, (request, response) => {
  database.login(request.body.username, request.body.password)
  .then((user) => {
    response.cookie(userCookie, user).jsonp({success: true});
  });
  .catch((error) => response.jsonp({success: false, error: error}));
});

router.post('/register', urlEncoded, (request, response) => {
  database.register(request.body.username, request.body.email, request.body.password)
  .then(() => response.jsonp({success: true}));
  .catch((error) => response.jsonp({success: false, error: error}));
});

router.use(cookieParser());

router.get('/find/locations', (request, response) => {
  database.findLocations()
  .then((locations) => response.jsonp(locations));
  .catch((error) => response.jsonp({success: false, error: error}));
});

router.post('/add/location', urlEncoded, (request, response) => {
  database.addLocation(request.body)
  .then(() => response.jsonp({success: true}));
  .catch((error) => response.jsonp({success: false, error: error}));
});

router.post('/location/:id/enter', (request, response) => {
  Promise.try(function(){
    if(!request.cookies[userCookie])
      throw 'You aren\'t logged in.';

    return database.enterEvent(request.params.id, request.cookies[userCookie])
  }).then(() => response.jsonp({success: true}))
  .catch((error) => response.jsonp({success: false, error: error}));
});

module.exports = router;

@@ -580,14 +580,8 @@
 ) =%3E
- %7B%0A   
  res
@@ -637,15 +637,9 @@
 ue%7D)
-;%0A  %7D);
+)
 %0A  .
@@ -872,32 +872,57 @@
 (() =%3E response.
+cookie(userCookie, user).
 jsonp(%7Bsuccess: 
@@ -920,33 +920,32 @@
 success: true%7D))
-;
 %0A  .catch((error
@@ -1163,17 +1163,16 @@
 ations))
-;
 %0A  .catc
@@ -1387,17 +1387,16 @@
  true%7D))
-;
 %0A  .catc
@@ -1674,16 +1674,27 @@
 ams.id, 
+JSON.parse(
 request.
@@ -1713,16 +1713,20 @@
 Cookie%5D)
+.id)
 %0A  %7D).th

Update api.js