Advertisement
tomilia

Untitled

Mar 29th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. module.exports = function(app, passport,nodemailer) {
  2. var mysql = require('mysql');
  3. var dbconfig = require('../config/database');
  4. // using SendGrid's v3 Node.js Library
  5. // https://github.com/sendgrid/sendgrid-nodejs
  6.  
  7.  
  8. // =====================================
  9. // HOME PAGE (with login links) ========
  10. // =====================================
  11.  
  12. // =====================================
  13. // LOGIN ===============================
  14. // =====================================
  15. // show the login form
  16. app.get('/', function(req, res) {
  17. // render the page and pass in any flash data if it exists
  18.  
  19. res.render('index.ejs', { message: req.flash('loginMessage') });
  20. console.log("\nCcc:"+req.flash('loginMessage')+"\n");
  21. });
  22. app.get('/homepage',isLoggedIn, function(req, res) {
  23. // render the page and pass in any flash data if it exists
  24. res.render('homepage.ejs', { message: req.flash('loginMessage') ,user:req.user});
  25. console.log("\nCcc:"+req.flash('loginMessage')+"\n");
  26. });
  27. // process the login form
  28. app.post('/', passport.authenticate('local-login', {
  29. session:true,
  30. successRedirect : '/homepage', // redirect to the secure profile section
  31. failureRedirect : '/',
  32. failureFlash: true// redirect back to the signup page if there is an error
  33. // allow flash messages
  34. }),
  35. function(req, res) {
  36. if (req.body.remember) {
  37. req.session.cookie.maxAge = 1000 * 60 * 3;
  38. } else {
  39. req.session.cookie.expires = false;
  40. }
  41. res.redirect('/homepage');
  42. });
  43.  
  44. // =====================================
  45. // SIGNUP ==============================
  46. // =====================================
  47. // show the signup form
  48. app.post('/forgotpw', function(req, res, next) {
  49. var email=req.body.pwf;
  50.  
  51. var connection = mysql.createConnection(dbconfig.connection);
  52. connection.query('USE ' + dbconfig.database);
  53. connection.query("SELECT * FROM student WHERE email = ?",[email], function(err, rows){
  54. if (err)
  55. {
  56. console.log("yey");
  57. return 0;
  58. }
  59.  
  60. console.log("XXX:"+rows[0].email);
  61. var smtpTransport = nodemailer.createTransport({
  62. service: 'gmail',
  63. auth: {
  64. user: 'koekax@gmail.com',
  65. pass: 'a25480097'
  66. }
  67. });
  68. var mailOptions = {
  69. to: rows[0].email,
  70. from: 'koekax@gmail.com',
  71. subject: 'MoneyPig Password Reset',
  72. text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
  73. 'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
  74. rows[0].password+
  75. ' If you did not request this, please ignore this email and your password will remain unchanged.\n'
  76. };
  77. smtpTransport.sendMail(mailOptions, function(err,info) {
  78. if(err)
  79. console.log(err)
  80. else
  81. console.log(info);
  82. });
  83. // all is well, return successful user
  84. });
  85. });
  86.  
  87.  
  88. app.get('/registration', function(req, res) {
  89. // render the page and pass in any flash data if it exists
  90. res.render('registration.ejs', { message: req.flash('signupMessage') });
  91. });
  92.  
  93. // process the signup form
  94. app.post('/registration', passport.authenticate('local-signup', {
  95. successRedirect : '/', // redirect to the secure profile section
  96. failureRedirect : '/registration', // redirect back to the signup page if there is an error
  97. failureFlash : true // allow flash messages
  98. }));
  99.  
  100. // =====================================
  101. // PROFILE SECTION =========================
  102. // =====================================
  103. // we will want this protected so you have to be logged in to visit
  104. // we will use route middleware to verify this (the isLoggedIn function)
  105.  
  106. // =====================================
  107. // LOGOUT ==============================
  108. // =====================================
  109. app.get('/logout', function(req, res) {
  110. req.logout();
  111. res.redirect('/');
  112. });
  113. };
  114.  
  115. // route middleware to make sure
  116. function isLoggedIn(req, res, next) {
  117. console.log("scpalc:"+req.isAuthenticated());
  118. // if user is authenticated in the session, carry on
  119. if (req.isAuthenticated())
  120. return next();
  121.  
  122. // if they aren't redirect them to the home page
  123. res.redirect('/');
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement