1

Here's the code for a new user:

var User = mongoose.model('User', userSchema);
var usr = new User({ username: 'bob', email: '[email protected]', password: 'secret' });

Here's the code for checking login.

passport.use(new LocalStrategy(function(username, password, done) {
  User.findOne({ username: username }, function(err, user) {
    if (err) { return done(err); }
    if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
    user.comparePassword(password, function(err, isMatch) {
      if (err) return done(err);
      if(isMatch) {
        return done(null, user);
      } else {
        return done(null, false, { message: 'Invalid password' });
      }
    });
  });
}));

If the username doesn't exist, it says "Unknown user __________"

Instead of saying unknown user, I want to create a new user in the database. How do I modify this code to do that?

I'd like to create a new user with the login info they entered if that login name doesn't already exist.

Update

I'm trying this and it's not working. bob5 isn't saving to the database.

passport.use(new LocalStrategy(function(username, password, done) {
  User.findOne({ username: username }, function(err, user) {
    if (err) { return done(err); }
 if (!user) { usr = new User({ username: 'bob5', email: '[email protected]', password: 'secret' });
usr.save(function(err) {
  if(err) {
    console.log(err);
  } else {
    console.log('user: ' + usr.username + " saved.");
  }
});

If I type this, bob99 gets saved to the database. So I can create a user... I just need to pass the arguments to it within the if statement (I think).

usr = new User({ username: 'bob99', email: '[email protected]', password: 'secret' });
usr.save(function(err) {
  if(err) {
    console.log(err);
  } else {
    console.log('user: ' + usr.username + " saved.");
  }
});
3
  • 1
    So? Instead of return done(...) do an insert. Commented Oct 26, 2013 at 5:42
  • I don't know how to do the "new User" command where it says return done. Can you please show me the proper syntax? I'll give you an upvote :) Commented Oct 26, 2013 at 5:46
  • Don't I also need to pass User in there as a parameter? Commented Oct 26, 2013 at 7:25

1 Answer 1

7
passport.use(new LocalStrategy(function(username, password, done) {

  User.findOne({ username: username }, function(err, user) {

    if (err) { return done(err); }

    if (!user) { 
         usr = new User({ username: 'bob99', email: '[email protected]', password: 'secret' });
         usr.save(function(err) {
         if(err) {
               console.log(err);
         } else {
               console.log('user: ' + usr.username + " saved.");
         }
      });

    }

    user.comparePassword(password, function(err, isMatch) {
      if (err) return done(err);
      if(isMatch) {
        return done(null, user);
      } else {
        return done(null, false, { message: 'Invalid password' });
      }
    });
  });
}));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Vardan! Can you please include the code to create the user? I'm not exactly sure how to pass the arguments/parameters.
In your above updated code where you are trying to save bob5, parenthesis are not properly closed. Check them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.