Guest User

Stackoverflow question

a guest
May 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // config/passport.js
  2.  
  3. // =========================================================================
  4. // LOCAL LOGIN =============================================================
  5. // =========================================================================
  6. // we are using named strategies since we have one for login and one for signup
  7. // by default, if there was no name, it would just be called 'local'
  8. // config/passport.js
  9.  
  10. // load all the things we need
  11. var LocalStrategy = require('passport-local').Strategy
  12. // var bcrypt = require('bcrypt')
  13. var bcrypt = require('bcryptjs')
  14. // load up the user model
  15. var mysql = require('mysql')
  16. const dbConfig = require('./db')
  17.  
  18. // expose this function to our app using module.exports
  19. module.exports = function (passport) {
  20.  
  21.   // =========================================================================
  22.   // passport session setup ==================================================
  23.   // =========================================================================
  24.   // required for persistent login sessions
  25.   // passport needs ability to serialize and unserialize users out of session
  26.  
  27.   // used to serialize the user for the session
  28.   passport.serializeUser(function (user, done) {
  29.     done(null, user.id);
  30.   });
  31.  
  32.   // used to deserialize the user
  33.   passport.deserializeUser(function (id, done) {
  34.     const connection = mysql.createConnection(dbConfig)
  35.     connection.connect(function(err) {
  36.       if (err) return done(err)
  37.       connection.query('SELECT * FROM users WHERE id = ?', [id], function (err, results) {
  38.         if (err) return done(err)
  39.         connection.end()
  40.         done(null, results.length === 1 ? results[0] : null);
  41.       });
  42.     })
  43.   });
  44.  
  45.   passport.use('local-login', new LocalStrategy({
  46.       usernameField : 'username',
  47.       passwordField : 'password',
  48.       passReqToCallback: true // allows us to pass back the entire request to the callback
  49.     },
  50.     function (req, username, password, done) { // callback with username and password from our form
  51.         req.getConnection(function(err, connection) {
  52.           if (err)  {
  53.             console.log('Mysql connection has not been established ', err)
  54.             return done(err);
  55.           }
  56.  
  57.           connection.query('SELECT * FROM users WHERE username = ?', [username], function(err, results) {
  58.             if (err) {
  59.               console.log('Sql query error ' + err)
  60.               return done(err);
  61.             }
  62.  
  63.             if(!results || results.length !== 1) {
  64.               console.log('User not found ', username)
  65.               return done(null, false, req.flash('loginMessage', 'Invalid credentials #1'))
  66.  
  67.             }
  68.  
  69.             bcrypt.compare(password, results[0].password, function(err, passwordsMatch) {
  70.               if (err || !passwordsMatch) {
  71.                 console.log('Password validation error ', err, passwordsMatch)
  72.                 return done(null, false, req.flash('loginMessage', 'Invalid credentials'));
  73.               }
  74.               // all is well, return successful user
  75.               return done(null, results[0]);
  76.             })
  77.           });
  78.  
  79.         });
  80.     }))
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment