Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using passport in node to register or create a user
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'});
});
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'});
});
@wald-tq
Copy link

wald-tq commented May 3, 2019

So, with that solution if a user has a typo in his email at login there will be a new user created?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment