简体   繁体   English

使用MongoDB和Node.JS使用Passport-Local创建新用户

[英]Create New User with Passport-Local using MongoDB and Node.JS

Here's the code for a new user : 这是新用户的代码:

var User = mongoose.model('User', userSchema);
var usr = new User({ username: 'bob', email: 'bob@example.com', password: 'secret' });

Here's the code for checking login . 这是检查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 __________" 如果username不存在,则显示"Unknown user __________"

Instead of saying unknown user , I want to create a new user in the database. 我不想说unknown user ,而是要在数据库中创建一个新用户。 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. bob5没有保存到数据库。

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: 'bob5@example.com', 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. 如果键入此内容,则bob99将保存到数据库。 So I can create a user... I just need to pass the arguments to it within the if statement (I think). 所以我可以创建一个用户...我只需要在if语句中传递参数给它(我认为)。

usr = new User({ username: 'bob99', email: 'bob99@example.com', password: 'secret' });
usr.save(function(err) {
  if(err) {
    console.log(err);
  } else {
    console.log('user: ' + usr.username + " saved.");
  }
});
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: 'bob99@example.com', 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' });
      }
    });
  });
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在node.js中使用passport-local和bcrypt检查和更新密码 - Checking and updating passwords with passport-local and bcrypt in node.js 通过 Passport-local mongoose node.js 支持多种用户类型 - Support for multiple user types by Passport-local mongoose node.js 如何使用Express,本地护照在Node.js中使用API​​令牌进行API调用 - How to make API call using API token in Node.js using express, passport-local 对于带有Node.js的Passport-Local,是否可以使用电子邮件而不是用户名进行身份验证? - For Passport-Local with Node.js, is it possible to authenticate with email rather than username? 没有使用本地护照策略在Node JS中调用passport.use方法 - passport.use method not calling in node js using passport-local strategy 用于护照本地注册表单的Node.js app.post不起作用 - Node.js app.post for passport-local signup form not working 如何使用passport.js的本地护照或其他Node.js工具,我可以拥有相当于完整的CRUD? - How, with passport.js's passport-local or other Node.js tools can I have the equivalent of full CRUD? 创建用户时护照本地超时(Node、Express、Postgres、Knex) - Passport-local times out on create user (Node, Express, Postgres, Knex) Sails.js 节点服务器 req.session 始终为空,使用 Passport-local 策略。 - Sails.js Node server req.session is always empty, using Passport-local strategy. 在Node JS中使用本地护照模块对密码进行身份验证(登录)时出错 - Error while authenticating(sign in) password using passport-local module in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM