var express = require('express');
var logger = require('morgan');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var routes = require('./routes/index');
var models = require('./models');
var helpers = require('./util/helpers');
var listeners = require('./util/listeners');

var app = express();

app.set('views', path.join(__dirname, '../front/views'));
app.set('view engine', 'jade');

// Enable CORS
app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

app.use(favicon(path.join(__dirname, '../front/static', 'favicon.ico')));

app.use(logger('dev'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(cookieParser());

app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: false,
}));

app.use(passport.initialize());
app.use(passport.session());

passport.use(new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    // session: false,
  }, function(username, password, done) {
    models.User.findOne({
      where: {
        username: username
      }
    }).then(function(user) {
      if (!user) {
        return done(null, false, {
          message: 'Incorrect username.'
        });
      }
      if (!user.validPassword(password)) {
        return done(null, false, {
          message: 'Incorrect password.'
        });
      }
      return done(null, user);
    }).catch(function (err) {
      console.log('Passport error:', err);
    });
  }
));

passport.serializeUser(function(user, done) {
  done(null, user.username);
});

passport.deserializeUser(function(username, done) {
  models.User.findOne({
    where: {
      username: username
    }
  }).then(function(user) {
    done(null, user);
  });
});

app.use(express.static(path.join(__dirname, '../front/static')));

app.use('/', routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// development error handler, will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler, no stacktraces leaked to user
app.use(function(err, req, res) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

var port = helpers.normalizePort(process.env.PORT || '3000');
app.set('port', port);

models.sequelize.sync()
  .then(function() {
    app.listen(port);
    app.on('error', listeners.onError);
    app.on('listening', listeners.onListening);
    console.log('Server running on port ' + port);
  });

module.exports = app;

@@ -1231,18 +1231,16 @@
 ategy(%7B%0A
-  
   userna
@@ -1260,18 +1260,16 @@
 rname',%0A
-  
   passwo
@@ -1312,18 +1312,16 @@
  false,%0A
-  
 %7D, funct
@@ -1352,18 +1352,16 @@
 done) %7B%0A
-  
   models
@@ -1380,18 +1380,16 @@
 e(%7B%0A    
-  
 where: %7B
@@ -1385,26 +1385,24 @@
    where: %7B%0A
-  
       userna
@@ -1418,22 +1418,18 @@
 ame%0A    
-  %7D%0A  
+%7D%0A
   %7D).the
@@ -1443,26 +1443,24 @@
 ion(user) %7B%0A
-  
     if (!use
@@ -1462,34 +1462,32 @@
 (!user) %7B%0A      
-  
 return done(null
@@ -1489,34 +1489,32 @@
 (null, false, %7B%0A
-  
         message:
@@ -1538,34 +1538,32 @@
 ame.'%0A      
- 
- 
 %7D);%0A    
   %7D%0A      if
@@ -1546,30 +1546,26 @@
     %7D);%0A    
-  %7D%0A  
+%7D%0A
     if (!use
@@ -1595,26 +1595,24 @@
 d)) %7B%0A      
- 
- 
 return done(
@@ -1622,26 +1622,24 @@
 l, false, %7B%0A
-  
         mess
@@ -1671,26 +1671,24 @@
 '%0A      
-  
 %7D);%0A    
   %7D%0A    
@@ -1679,22 +1679,18 @@
 %7D);%0A    
- 
- 
 %7D%0A
-  
     retu
@@ -1706,26 +1706,24 @@
 ull, user);%0A
-  
   %7D).catch(f
@@ -1742,18 +1742,16 @@
 ) %7B%0A    
- 
- 
 console.
@@ -1779,26 +1779,22 @@
 , err);%0A
-  
   %7D);%0A
-  
 %7D%0A));%0A%0Ap

fix indent on main app module