//Functions to handle most routes
var db = require('../utilities/database');
var passport = require('passport');

passport.deserializeUser(function(id, done) {
  db.Users.findById(id, function(err, user) {
    done(err, user);
  }); 
});


/*
* Render the user profile
*/
var profile = function(req, res, next){
	if(req.isAuthenticated()){
		db.Users.findOne({_id: req.user._id}, function(err, user){
			db.Books.find({userID: req.user._id}, function(err, books){
				var data = {};
				data.user = user;
				data.books = books;
				// console.log(data);
				user.auth = true;
				res.render('profile', data);
			})
			
		})
	}
	else{
		req.session.returnTo = req.route.path;
		res.redirect('/');
	}
}

exports.profileUpdate = function(req,res,next){
	if(req.isAuthenticated()){
		console.log(req.body);
		// console.log(req);
		db.Users.findOne({_id: req.user._id}, function(err, user){
			console.log(user);
			user.name = req.body.name;
			var phone = req.body.phone.replace(/[^\d]/g, "");
			user.email = req.body.email;
			user.phone = phone;
			user.save(function(err, data){
				if(err){
						console.log(err);
						next();
						res.json(400);
					}
				else
					res.redirect('/profile')
			})
		})
	}
}
/*
* Render the user settings page
*/
var userData = function(req, res, next){
	if(req.isAuthenticated()){
		data = {
			email : req.user.email,
			phone : req.user.phone
		}
		data.authentification = true;
		res.render('profile', data);
	}
	else{
		req.session.returnTo = req.route.path;
		res.redirect('/signin');
	}		
}

/*
* Render view to submit a new entry
*/
var newEntry = function(req, res, next){
	if(req.isAuthenticated()){
		if(req.user.email === ""){
			res.redirect('/perfil');
		}
		else{
		var currentFile = {};
		currentFile.authentification = true;
		res.render('newEntry', currentFile);
		}
	}
	else{
		req.session.returnTo = req.route.path;
		res.redirect('/entrar');
	}
}

/*
* Render the sign in view
*/
var signIn = function(req,res){
	if(req.isAuthenticated()){
		res.redirect('/galeria');
	}
	else{
		res.render('signin');
	}
}


//Export Functions
exports.newEntry = newEntry;
exports.signIn = signIn;
exports.profile = profile;
exports.userData = userData;

@@ -297,32 +297,129 @@
 eq, res, next)%7B%0A
+%09// var auth = req.isAuthenticated();%0A%09// console.log(auth);%0A%09// var content = %7B'auth': auth%7D;%0A%09%0A
 %09if(req.isAuthen
@@ -649,20 +649,20 @@
 a);%0A%09%09%09%09
-user
+data
 .auth = 
@@ -667,16 +667,39 @@
 = true;%0A
+%09%09%09%09console.log(data);%0A
 %09%09%09%09res.

fix header auth btn