Skip to content

Instantly share code, notes, and snippets.

@technotim
Created January 5, 2017 04:20
Show Gist options
  • Save technotim/0a8b9186bf0659e9adfc973a3a97e2e1 to your computer and use it in GitHub Desktop.
Save technotim/0a8b9186bf0659e9adfc973a3a97e2e1 to your computer and use it in GitHub Desktop.
nodejs authentication mysql passport
// config/passport.js
// -----------------------------------------------------------------------
// Original script from RisingStack nodehero-authentication tutorial
// https://blog.risingstack.com/node-hero-node-js-authentication-passport-js/
// Mysql conversion by manjeshpv
// https://gist.github.com/manjeshpv/84446e6aa5b3689e8b84
// My version minimizes changes needed to the original passport.js file
// Revision Date: 1/4/17
// -----------------------------------------------------------------------
// load all the things we need
var LocalStrategy = require('passport-local').Strategy;
var User = require('../app/models/user-mysql')
// expose this function to our app using module.exports
module.exports = function(passport) {
// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user['id']) //changed user.id to user['id']
})
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user){
done(err, user)
})
})
// =========================================================================
// LOCAL SIGNUP ============================================================
// =========================================================================
// we are using named strategies since we have one for login and one for signup
// by default, if there was no name, it would just be called 'local'
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
process.nextTick(function(){
User.findOne('email', email, function(err, user){ //Note change from JSON to single variables
if (err)
return done(err)
if (user) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'))
} else {
// if there is no user with that email create the user
var newUser = new Object() //Not using mongoose User schema so just write newUser to object
newUser.email = email
newUser.password = User.generateHash(password) //Uses generateHash, and save functions from User
User.save(newUser, function(err){
if(err)
throw err
return done(null, newUser)
})
}
})
})
}))
// =========================================================================
// LOCAL LOGIN =============================================================
// =========================================================================
// we are using named strategies since we have one for login and one for signup
// by default, if there was no name, it would just be called 'local'
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
User.findOne('email', email, function(err, user){
if (err)
return done(err)
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')) // req.flash is the way to set flashdata using connect-flash
if (!User.validPassword(password, user['password']))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')) // create the loginMessage and save it to session as flashdata
return done(null, user)
})
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment