module.exports = function(app, passport) { var User = require('../app/models/user'); // normal routes =============================================================== // show the home page (will also have our login links) app.get('/', function(req, res) { res.render('index.html'); }); // PROFILE SECTION ========================= app.get('/profile', isLoggedIn, function(req, res) { res.render('profile.html', { user : req.user }); console.log(req.user); }); /*user mod profile*/ app.get('/add_information', isLoggedIn, function(req,res){ res.render('add_information.html', { user : req.user }); }); /*add information in db*/ /*Todo surname ecc..*/ app.post('/add_information', isLoggedIn, function(req,res){ User.findOneAndUpdate({_id:req.user._id},{"name":req.body.name},{upsert:true},function(err,user){ console.log("ok!"+req.body.name); res.redirect('/profile'); }); }); // LOGOUT ============================== app.get('/logout', function(req, res) { req.logout(); res.redirect('/'); }); // ============================================================================= // AUTHENTICATE (FIRST LOGIN) ================================================== // ============================================================================= // locally -------------------------------- // LOGIN =============================== // show the login form app.get('/login', function(req, res) { res.render('login.html', { message: req.flash('loginMessage') }); }); // process the login form app.post('/login', passport.authenticate('local-login', { successRedirect : '/profile', // redirect to the secure profile section failureRedirect : '/login', // redirect back to the signup page if there is an error failureFlash : true // allow flash messages })); // SIGNUP ================================= // show the signup form app.get('/signup', function(req, res) { res.render('signup.html', { message: req.flash('signupMessage') }); }); /*aggiunta informazioni utente*/ app.get('/signup2', function(req, res) { res.render('signup2.html'); }); app.post('/signup2',function(req, res){ User.findOneAndUpdate({_id:req.user._id},{ "name":req.body.name, "surname":req.body.surname, "role":req.body.role},{upsert:true},function(err,user){ if (req.body.role == 'seller') { res.render('signup3_seller.html'); } if (req.body.role == 'buyer') { res.render('signup3_buyer.html'); } //res.redirect('/profile'); }); }); app.post('/signup3_buyer',function(req,res){ User.findOneAndUpdate({_id:req.user._id},{ "company_name":req.body.company_name, "description":req.body.description, "location":req.body.location, "country":req.body.setting_country, "country_code":req.body.setting_country_short, "state":req.body.setting_state, "state_code":req.body.setting_state_short, "city":req.body.setting_city, "latitude":req.body.setting_latitude, "longitude":req.body.setting_longitude },{upsert:true},function(err,user){ res.render('signup4.html'); }); }); app.get('/signup4',function(req, res) { res.render('signup4.html'); }); app.post('/signup4',function(req, res) { console.log(req.files.fileToUpload.path); /*cloudinary.uploader.upload(req.files.profile.path, function(result) { console.log(result); });*/ }); // process the signup form app.post('/signup', passport.authenticate('local-signup', { successRedirect : '/signup2', // redirect to the secure profile section failureRedirect : '/signup', // redirect back to the signup page if there is an error failureFlash : true // allow flash messages })); // facebook ------------------------------- // send to facebook to do the authentication app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); // handle the callback after facebook has authenticated the user app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect : '/profile', failureRedirect : '/' })); // twitter -------------------------------- // send to twitter to do the authentication app.get('/auth/twitter', passport.authenticate('twitter', { scope : 'email' })); // handle the callback after twitter has authenticated the user app.get('/auth/twitter/callback', passport.authenticate('twitter', { successRedirect : '/profile', failureRedirect : '/' })); // google --------------------------------- // send to google to do the authentication app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] })); // the callback after google has authenticated the user app.get('/auth/google/callback', passport.authenticate('google', { successRedirect : '/profile', failureRedirect : '/' })); // ============================================================================= // AUTHORIZE (ALREADY LOGGED IN / CONNECTING OTHER SOCIAL ACCOUNT) ============= // ============================================================================= // locally -------------------------------- app.get('/connect/local', function(req, res) { res.render('connect-local.html', { message: req.flash('loginMessage') }); }); app.post('/connect/local', passport.authenticate('local-signup', { successRedirect : '/profile', // redirect to the secure profile section failureRedirect : '/connect/local', // redirect back to the signup page if there is an error failureFlash : true // allow flash messages })); // facebook ------------------------------- // send to facebook to do the authentication app.get('/connect/facebook', passport.authorize('facebook', { scope : 'email' })); // handle the callback after facebook has authorized the user app.get('/connect/facebook/callback', passport.authorize('facebook', { successRedirect : '/profile', failureRedirect : '/' })); // twitter -------------------------------- // send to twitter to do the authentication app.get('/connect/twitter', passport.authorize('twitter', { scope : 'email' })); // handle the callback after twitter has authorized the user app.get('/connect/twitter/callback', passport.authorize('twitter', { successRedirect : '/profile', failureRedirect : '/' })); // google --------------------------------- // send to google to do the authentication app.get('/connect/google', passport.authorize('google', { scope : ['profile', 'email'] })); // the callback after google has authorized the user app.get('/connect/google/callback', passport.authorize('google', { successRedirect : '/profile', failureRedirect : '/' })); // ============================================================================= // UNLINK ACCOUNTS ============================================================= // ============================================================================= // used to unlink accounts. for social accounts, just remove the token // for local account, remove email and password // user account will stay active in case they want to reconnect in the future // local ----------------------------------- app.get('/unlink/local', isLoggedIn, function(req, res) { var user = req.user; user.local.email = undefined; user.local.password = undefined; user.save(function(err) { res.redirect('/profile'); }); }); // facebook ------------------------------- app.get('/unlink/facebook', isLoggedIn, function(req, res) { var user = req.user; user.facebook.token = undefined; user.save(function(err) { res.redirect('/profile'); }); }); // twitter -------------------------------- app.get('/unlink/twitter', isLoggedIn, function(req, res) { var user = req.user; user.twitter.token = undefined; user.save(function(err) { res.redirect('/profile'); }); }); // google --------------------------------- app.get('/unlink/google', isLoggedIn, function(req, res) { var user = req.user; user.google.token = undefined; user.save(function(err) { res.redirect('/profile'); }); }); }; // route middleware to ensure user is logged in function isLoggedIn(req, res, next) { if (req.isAuthenticated()){ return next(); } res.redirect('/'); }