Created
November 18, 2012 23:25
-
-
Save christopheretcheverry/4108050 to your computer and use it in GitHub Desktop.
Using passport in node to register or create a user
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
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use(new LocalStrategy( | |
function(username, password, done) { | |
//Search for user | |
User.find({ where: {email: username} }).success(function(user) { | |
//If no user register a new one | |
if (!user) { | |
var today = new Date(); | |
var salt = today.getTime(); | |
var createdDate = today.toUTCString(); | |
var newPass = crypto.hashPassword(password, salt); | |
var user = User.build({ | |
email: username, | |
password: newPass, | |
salt: salt | |
}); | |
user.save().success(function(savedUser) { | |
console.log('Saved user successfully: %j', savedUser); | |
return done(null, savedUser); | |
}).error(function(error) { | |
console.log(error); | |
return done(null, false, { message: 'Something went wrong in registration' }); | |
}); | |
} | |
//Found user check password | |
if (!crypto.validPassword(password, user)) { | |
console.log('In password check'); | |
return done(null, false, { message: 'Invalid password' }); | |
} | |
console.log("Out local strategy"); | |
return done(null, user); | |
}); | |
} | |
)); | |
app.post('/login', passport.authenticate('local'), function(req, res) { | |
console.log('Logging in as: ' + req.user); | |
res.send({success: 'success'}); | |
}); |
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
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use(new LocalStrategy( | |
function(username, password, done) { | |
console.log("In local strategy"); | |
//Search for user | |
User.find({ where: {email: username} }).success(function(user) { | |
//If no user register a new one | |
if (!user) { | |
console.log("Registering new user"); | |
var today = new Date(); | |
var salt = today.getTime(); | |
var createdDate = today.toUTCString(); | |
var newPass = crypto.hashPassword(password, salt); | |
var user = User.build({ | |
email: username, | |
password: newPass, | |
salt: salt | |
}); | |
user.save().success(function(savedUser) { | |
console.log('Saved user successfully: %j', savedUser); | |
return done(null, savedUser); | |
}).error(function(error) { | |
console.log(error); | |
return done(null, false, { message: 'Something went wrong in registration' }); | |
}); | |
return done(null, false, { message: 'Unknown user' }); | |
} | |
console.log('out register'); | |
//Found user check password | |
if (!crypto.validPassword(password, user)) { | |
console.log('In password check'); | |
return done(null, false, { message: 'Invalid password' }); | |
} | |
console.log("Out local strategy"); | |
return done(null, user); | |
}); | |
} | |
)); | |
app.post('/login', passport.authenticate('local'), function(req, res) { | |
console.log('Logging in as: ' + req.user); | |
res.send({success: 'success'}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, with that solution if a user has a typo in his email at login there will be a new user created?