Guest User

Untitled

a guest
Apr 5th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. module.exports = function(app, passport) {
  2. var User = require('../app/models/user');
  3. // normal routes ===============================================================
  4.  
  5. // show the home page (will also have our login links)
  6. app.get('/', function(req, res) {
  7. res.render('index.html');
  8. });
  9.  
  10. // PROFILE SECTION =========================
  11. app.get('/profile', isLoggedIn, function(req, res) {
  12. res.render('profile.html', {
  13. user : req.user
  14. });
  15. console.log(req.user);
  16. });
  17. /*user mod profile*/
  18. app.get('/add_information', isLoggedIn, function(req,res){
  19. res.render('add_information.html', {
  20. user : req.user
  21. });
  22. });
  23. /*add information in db*/
  24. /*Todo surname ecc..*/
  25. app.post('/add_information', isLoggedIn, function(req,res){
  26. User.findOneAndUpdate({_id:req.user._id},{"name":req.body.name},{upsert:true},function(err,user){
  27. console.log("ok!"+req.body.name);
  28. res.redirect('/profile');
  29. });
  30. });
  31.  
  32. // LOGOUT ==============================
  33. app.get('/logout', function(req, res) {
  34. req.logout();
  35. res.redirect('/');
  36. });
  37.  
  38. // =============================================================================
  39. // AUTHENTICATE (FIRST LOGIN) ==================================================
  40. // =============================================================================
  41.  
  42. // locally --------------------------------
  43. // LOGIN ===============================
  44. // show the login form
  45. app.get('/login', function(req, res) {
  46. res.render('login.html', { message: req.flash('loginMessage') });
  47. });
  48.  
  49. // process the login form
  50. app.post('/login', passport.authenticate('local-login', {
  51. successRedirect : '/profile', // redirect to the secure profile section
  52. failureRedirect : '/login', // redirect back to the signup page if there is an error
  53. failureFlash : true // allow flash messages
  54. }));
  55.  
  56. // SIGNUP =================================
  57. // show the signup form
  58. app.get('/signup', function(req, res) {
  59. res.render('signup.html', { message: req.flash('signupMessage') });
  60. });
  61. /*aggiunta informazioni utente*/
  62. app.get('/signup2', function(req, res) {
  63. res.render('signup2.html');
  64. });
  65. app.post('/signup2',function(req, res){
  66. User.findOneAndUpdate({_id:req.user._id},{
  67. "name":req.body.name,
  68. "surname":req.body.surname,
  69. "role":req.body.role},{upsert:true},function(err,user){
  70. if (req.body.role == 'seller') {
  71. res.render('signup3_seller.html');
  72. }
  73. if (req.body.role == 'buyer') {
  74. res.render('signup3_buyer.html');
  75. }
  76. //res.redirect('/profile');
  77. });
  78. });
  79. app.post('/signup3_buyer',function(req,res){
  80. User.findOneAndUpdate({_id:req.user._id},{
  81. "company_name":req.body.company_name,
  82. "description":req.body.description,
  83. "location":req.body.location,
  84. "country":req.body.setting_country,
  85. "country_code":req.body.setting_country_short,
  86. "state":req.body.setting_state,
  87. "state_code":req.body.setting_state_short,
  88. "city":req.body.setting_city,
  89. "latitude":req.body.setting_latitude,
  90. "longitude":req.body.setting_longitude
  91. },{upsert:true},function(err,user){
  92. res.render('signup4.html');
  93. });
  94. });
  95. app.get('/signup4',function(req, res) {
  96. res.render('signup4.html');
  97. });
  98.  
  99. app.post('/signup4',function(req, res) {
  100. console.log(req.files.fileToUpload.path);
  101. /*cloudinary.uploader.upload(req.files.profile.path, function(result) {
  102. console.log(result);
  103. });*/
  104. });
  105.  
  106. // process the signup form
  107. app.post('/signup', passport.authenticate('local-signup', {
  108. successRedirect : '/signup2', // redirect to the secure profile section
  109. failureRedirect : '/signup', // redirect back to the signup page if there is an error
  110. failureFlash : true // allow flash messages
  111. }));
  112.  
  113. // facebook -------------------------------
  114.  
  115. // send to facebook to do the authentication
  116. app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
  117.  
  118. // handle the callback after facebook has authenticated the user
  119. app.get('/auth/facebook/callback',
  120. passport.authenticate('facebook', {
  121. successRedirect : '/profile',
  122. failureRedirect : '/'
  123. }));
  124.  
  125. // twitter --------------------------------
  126.  
  127. // send to twitter to do the authentication
  128. app.get('/auth/twitter', passport.authenticate('twitter', { scope : 'email' }));
  129.  
  130. // handle the callback after twitter has authenticated the user
  131. app.get('/auth/twitter/callback',
  132. passport.authenticate('twitter', {
  133. successRedirect : '/profile',
  134. failureRedirect : '/'
  135. }));
  136.  
  137.  
  138. // google ---------------------------------
  139.  
  140. // send to google to do the authentication
  141. app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] }));
  142.  
  143. // the callback after google has authenticated the user
  144. app.get('/auth/google/callback',
  145. passport.authenticate('google', {
  146. successRedirect : '/profile',
  147. failureRedirect : '/'
  148. }));
  149.  
  150. // =============================================================================
  151. // AUTHORIZE (ALREADY LOGGED IN / CONNECTING OTHER SOCIAL ACCOUNT) =============
  152. // =============================================================================
  153.  
  154. // locally --------------------------------
  155. app.get('/connect/local', function(req, res) {
  156. res.render('connect-local.html', { message: req.flash('loginMessage') });
  157. });
  158. app.post('/connect/local', passport.authenticate('local-signup', {
  159. successRedirect : '/profile', // redirect to the secure profile section
  160. failureRedirect : '/connect/local', // redirect back to the signup page if there is an error
  161. failureFlash : true // allow flash messages
  162. }));
  163.  
  164. // facebook -------------------------------
  165.  
  166. // send to facebook to do the authentication
  167. app.get('/connect/facebook', passport.authorize('facebook', { scope : 'email' }));
  168.  
  169. // handle the callback after facebook has authorized the user
  170. app.get('/connect/facebook/callback',
  171. passport.authorize('facebook', {
  172. successRedirect : '/profile',
  173. failureRedirect : '/'
  174. }));
  175.  
  176. // twitter --------------------------------
  177.  
  178. // send to twitter to do the authentication
  179. app.get('/connect/twitter', passport.authorize('twitter', { scope : 'email' }));
  180.  
  181. // handle the callback after twitter has authorized the user
  182. app.get('/connect/twitter/callback',
  183. passport.authorize('twitter', {
  184. successRedirect : '/profile',
  185. failureRedirect : '/'
  186. }));
  187.  
  188.  
  189. // google ---------------------------------
  190.  
  191. // send to google to do the authentication
  192. app.get('/connect/google', passport.authorize('google', { scope : ['profile', 'email'] }));
  193.  
  194. // the callback after google has authorized the user
  195. app.get('/connect/google/callback',
  196. passport.authorize('google', {
  197. successRedirect : '/profile',
  198. failureRedirect : '/'
  199. }));
  200.  
  201. // =============================================================================
  202. // UNLINK ACCOUNTS =============================================================
  203. // =============================================================================
  204. // used to unlink accounts. for social accounts, just remove the token
  205. // for local account, remove email and password
  206. // user account will stay active in case they want to reconnect in the future
  207.  
  208. // local -----------------------------------
  209. app.get('/unlink/local', isLoggedIn, function(req, res) {
  210. var user = req.user;
  211. user.local.email = undefined;
  212. user.local.password = undefined;
  213. user.save(function(err) {
  214. res.redirect('/profile');
  215. });
  216. });
  217.  
  218. // facebook -------------------------------
  219. app.get('/unlink/facebook', isLoggedIn, function(req, res) {
  220. var user = req.user;
  221. user.facebook.token = undefined;
  222. user.save(function(err) {
  223. res.redirect('/profile');
  224. });
  225. });
  226.  
  227. // twitter --------------------------------
  228. app.get('/unlink/twitter', isLoggedIn, function(req, res) {
  229. var user = req.user;
  230. user.twitter.token = undefined;
  231. user.save(function(err) {
  232. res.redirect('/profile');
  233. });
  234. });
  235.  
  236. // google ---------------------------------
  237. app.get('/unlink/google', isLoggedIn, function(req, res) {
  238. var user = req.user;
  239. user.google.token = undefined;
  240. user.save(function(err) {
  241. res.redirect('/profile');
  242. });
  243. });
  244.  
  245.  
  246. };
  247.  
  248. // route middleware to ensure user is logged in
  249. function isLoggedIn(req, res, next) {
  250. if (req.isAuthenticated()){
  251. return next();
  252. }
  253. res.redirect('/');
  254. }
Add Comment
Please, Sign In to add comment