'use strict';

var passport = require('passport');
var User = require('../models').model('User');

module.exports = {
    deny : function(req, res) {
            res.sendStatus(405);
    },
    root : {
        get : function(req, res) {
            res.json('index', {
                title : (req.user && req.user.localName) || 'Nobody'
            });
        }
    },
    login : {
        post : passport.authenticate('local', {
            successRedirect : '/',
            failureRedirect : '/'
        })
    },
    logout : {
        all : function(req, res, next) {
            if(!req.user) {
                var err = new Error("Log in first.");
                return next(err);
            }

            req.logout();
            res.sendStatus(200);
        }
    },
    changePassword : {
        patch : function(req, res, next) {
            // check that user is logged in
            // check that body contains a password value
            if(!req.body || !req.user || !req.body.password) {
                var err = new Error("No password supplied.");
                return next(err);
            }
            // bcrypt the password
            req.user.setPassword(req.body.password).
                then(function() {
                    res.sendStatus(200);
                }).catch(function(err) {
                    next(err);
                });            
        }
    },
    signup : {
        post : function(req, res, next) {
            if(!req.body || !req.body.username || !req.body.password) {
                var err = new Error("No credentials.");
                return next(err);
            }

            var pUser = new Promise(function(res, rej) {
                User.create({
                    userName : req.body.username
                }, function(err, user) {
                    if(err) {
                        rej(err);
                        return;
                    }

                    res(user);
                });
            });
            pUser.then(function(user) {
                user.setPassword(req.body.password);
            }).catch(function(err) {
                next(err);
            });
        }
    }
};

@@ -2050,24 +2050,31 @@
             
+return 
 user.setPass
@@ -2098,16 +2098,86 @@
 sword);%0A
+            %7D).then(function() %7B%0A                res.sendStatus(200);%0A
         

Add server response to signup route