Created
October 10, 2015 16:10
-
-
Save ericpkatz/e5dbbe5ee1d811d2238d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var router = require('express').Router(); | |
var passport = require('passport'); | |
var LocalStrategy = require('passport-local').Strategy; | |
var User = require('../api/users/user.model'); | |
module.exports = router; | |
router.use(passport.initialize()); | |
router.use(passport.session()); | |
passport.serializeUser(function(user, done) { | |
console.log('Serializing', user); | |
done(null, user._id); | |
}); | |
passport.deserializeUser(function (id, done) { | |
console.log('DeSerializing', id); | |
User.findById(id) | |
.then(function(user){ | |
done(null, user); | |
}); | |
}); | |
passport.use(new LocalStrategy({ | |
usernameField: 'email' | |
},function(email, password, done){ | |
User.findOne({email: email}) | |
.then(function(user){ | |
if(!user) | |
return User.create({ | |
email: email, | |
password: password, | |
name: email.split('@')[0] | |
}); | |
if(user && user.password == password) | |
return user; | |
}) | |
.then(function(user){ | |
if(user){ | |
return done(null, user); | |
} | |
done(false, null); | |
}) | |
.catch(function(ex){ | |
done(ex, null); | |
}); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment