Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function(app, passport,nodemailer) {
- var mysql = require('mysql');
- var dbconfig = require('../config/database');
- // using SendGrid's v3 Node.js Library
- // https://github.com/sendgrid/sendgrid-nodejs
- // =====================================
- // HOME PAGE (with login links) ========
- // =====================================
- // =====================================
- // LOGIN ===============================
- // =====================================
- // show the login form
- app.get('/', function(req, res) {
- // render the page and pass in any flash data if it exists
- res.render('index.ejs', { message: req.flash('loginMessage') });
- console.log("\nCcc:"+req.flash('loginMessage')+"\n");
- });
- app.get('/homepage',isLoggedIn, function(req, res) {
- // render the page and pass in any flash data if it exists
- res.render('homepage.ejs', { message: req.flash('loginMessage') ,user:req.user});
- console.log("\nCcc:"+req.flash('loginMessage')+"\n");
- });
- // process the login form
- app.post('/', passport.authenticate('local-login', {
- session:true,
- successRedirect : '/homepage', // redirect to the secure profile section
- failureRedirect : '/',
- failureFlash: true// redirect back to the signup page if there is an error
- // allow flash messages
- }),
- function(req, res) {
- if (req.body.remember) {
- req.session.cookie.maxAge = 1000 * 60 * 3;
- } else {
- req.session.cookie.expires = false;
- }
- res.redirect('/homepage');
- });
- // =====================================
- // SIGNUP ==============================
- // =====================================
- // show the signup form
- app.post('/forgotpw', function(req, res, next) {
- var email=req.body.pwf;
- var connection = mysql.createConnection(dbconfig.connection);
- connection.query('USE ' + dbconfig.database);
- connection.query("SELECT * FROM student WHERE email = ?",[email], function(err, rows){
- if (err)
- {
- console.log("yey");
- return 0;
- }
- console.log("XXX:"+rows[0].email);
- var smtpTransport = nodemailer.createTransport({
- service: 'gmail',
- auth: {
- user: 'koekax@gmail.com',
- pass: 'a25480097'
- }
- });
- var mailOptions = {
- to: rows[0].email,
- from: 'koekax@gmail.com',
- subject: 'MoneyPig Password Reset',
- text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
- 'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
- rows[0].password+
- ' If you did not request this, please ignore this email and your password will remain unchanged.\n'
- };
- smtpTransport.sendMail(mailOptions, function(err,info) {
- if(err)
- console.log(err)
- else
- console.log(info);
- });
- // all is well, return successful user
- });
- });
- app.get('/registration', function(req, res) {
- // render the page and pass in any flash data if it exists
- res.render('registration.ejs', { message: req.flash('signupMessage') });
- });
- // process the signup form
- app.post('/registration', passport.authenticate('local-signup', {
- successRedirect : '/', // redirect to the secure profile section
- failureRedirect : '/registration', // redirect back to the signup page if there is an error
- failureFlash : true // allow flash messages
- }));
- // =====================================
- // PROFILE SECTION =========================
- // =====================================
- // we will want this protected so you have to be logged in to visit
- // we will use route middleware to verify this (the isLoggedIn function)
- // =====================================
- // LOGOUT ==============================
- // =====================================
- app.get('/logout', function(req, res) {
- req.logout();
- res.redirect('/');
- });
- };
- // route middleware to make sure
- function isLoggedIn(req, res, next) {
- console.log("scpalc:"+req.isAuthenticated());
- // if user is authenticated in the session, carry on
- if (req.isAuthenticated())
- return next();
- // if they aren't redirect them to the home page
- res.redirect('/');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement