'use strict';

var passport = require('passport');

module.exports = function (router) {

    router.get('/', function (req, res) {
		
        res.render('index');
        
    });
	
	router.post('/signup', passport.authenticate('local', {
        successRedirect : '/profile', // redirect to the secure user profile section
        failureRedirect : '/', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));
/*	
	router.post('/signin', passport.authenticate('local-sign-in', {
        successRedirect : '/home', // redirect to the secure home section
        failureRedirect : '/', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));
*/	
	// - Sign-in route handler for sign - //
	router.post('/signin', 
	passport.authenticate('local-sign-in'),
	function(req, res) {
		//If this function gets called, this means authentication was successful. 
		//The 'req.user' contains the autheicated user.
		//delete req.user.password; 
		delete req.user;
		//Deleted to avoid user information leak and to mantain normailized user data within application.
		
		//console.log(req.session);
		//console.log();

		res.redirect('/home'); //req.user.username;
			
	});
	
	// - Logout route from application. - //
	router.get('/logout', function(req, res){
	  //logic to clear session cookies and all info before exit.
		req.session.destroy(); //OR - delete req.session;
	  //req.logout();
	  res.redirect('/');
	});

/* THIS CAUSES PROBLEMS FOR SOME OF THE PAGES LOADING...
	// - This is a catch-all for requested pages that don't exist. - //
	router.use(function (req,res) {
    	res.render('errors/404', {url:req.url});
	});
*/	
};
	

@@ -495,16 +495,44 @@
 ignin', 
+function(req, res, next)%7B%0A%09%09
 passport
@@ -550,246 +550,423 @@
 te('
-local-
 sign
--i
+I
 n', 
-%7B%0A        successRedirect : '/home', // redirect to the secure home section%0A        failureRedirect : '/', // redirect back to the signup page if there is an error%0A        failureFlash : true // allow flash messages%0A    %7D)
+function(err, user, info)%7B%0A%09%09%09if(err)%7B%0A%09%09%09%09return next(err);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if(!user)%7B%0A%09%09%09%09console.log(info);%0A%09%09%09%09return res.render('/', %7Bmessage: info.signInMsg %7D);// Back to homepage on failure.%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09req.logIn(user, function(err) %7B%0A%09%09%09%09if(err) %7B %0A%09%09%09%09%09return next(err);%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09//return res.redirect('/users/' + user.username);%0A%09%09%09%09return res.redirect('/home');%0A%09%09%09%7D);%0A%09%09%7D)(req, res, next);%0A%09%7D
 );%0A*
@@ -1483,16 +1483,19 @@
 %09%09%0A%09%7D);%0A
+%0A%0A%0A
 %09%0A%09// - 
@@ -1728,22 +1728,24 @@
 t('/');%0A
-
 %09%7D);%0A%0A
+%0A%0A
 /* THIS 

Update signin rounte handler.