Skip to content

Instantly share code, notes, and snippets.

@dsomel21
Created September 24, 2016 17:04
Show Gist options
  • Save dsomel21/ff0c55a1f437a721ee126e244e115093 to your computer and use it in GitHub Desktop.
Save dsomel21/ff0c55a1f437a721ee126e244e115093 to your computer and use it in GitHub Desktop.
with mongoose and crpytion
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
module.exports = function(passport){
/* SERIALIZING & DESERIALIZING USERS */
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
User.findById(id, function(err, user) {
done(err, user);
});
});
/* PASSPORT LOCAL AUTHENTICATION */
passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
},
function(req, username, password, done){
console.log(username, password);
User.findOne({ 'local.email' : email }, function(err, user) {
if (err){
return done(err);
}
if (!user) {
console.log('no user found');
return done(null, false);
}
if (!user.validPassword(password)){
console.log('Oops! Wrong password.');
return done(null, false);
}
return done(null, user);
})
}));
passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done){
process.nextTick(function() {
/* Try and see if it can find that user at all */
// User.findOne({ 'local.email' : email }, function(err, user) {
// console.log('hi');
// console.log(user);
// if(err){
// console.log('error');
// return done(err);
// }
// if (user){
// console.log("already exists");
// return done(null, false, req.flash('signupMessage', 'E-Mail already exists!'));
// }
// else {
console.log("------------------");
var newUser = new User();
newUser.local.firstname = req.body.firstname;
newUser.local.lastname = req.body.lastname;
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
console.log(newUser);
/* Insert them into the database */
newUser.save(function(err){
if(err){
throw err;
}
return done (null, newUser);
});
// }
// });
});
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment