简体   繁体   English

新用户注册后立即进行Passport身份验证

[英]Passport Authentication immediately after New User Registration

I'm trying to authenticate and login a user immediately after submitting a POST on the /register form. 我正在尝试在/register表单上提交POST后立即进行身份验证并登录用户。 Ideally, I would like users to be able to register and then be redirected immediately to the dashboard without having to enter their credentials again. 理想情况下,我希望用户能够注册,然后立即重定向到仪表板,而无需再次输入凭据。

My server is using Passport 0.1.17 with the local strategy configured to use email address and password for login. 我的服务器使用Passport 0.1.17,本地策略配置为使用电子邮件地址和密码登录。 The current code is: 目前的代码是:

app.post('/register', function(req, res) {

  // attach POST to new User variable
  var registerUser = new User({ email: req.body.email, password: req.body.password, name: req.body.name });

  // save registerUser Mongo
  registerUser.save(function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log('registerUser: ' + registerUser.email + " saved.");
    }
  });

  // here is where I am trying to authenticate and then redirect
  passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
  res.redirect('/dashboard');
  });

How would I refactor this code to save the new user, then authenticate and finally redirect to the dashboard? 我如何重构此代码以保存新用户,然后进行身份验证并最终重定向到仪表板?

Thanks in advance! 提前致谢!

Here's the solution I came up with after reading about req.login : 这是我在阅读req.login后想出的解决方案:

app.post('/register', function(req, res) {
  // attach POST to user schema
  var user = new User({ email: req.body.email, password: req.body.password, name: req.body.name });
  // save in Mongo
  user.save(function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log('user: ' + user.email + " saved.");
      req.login(user, function(err) {
        if (err) {
          console.log(err);
        }
        return res.redirect('/dashboard');
      });
    }
  });
});

I would like to clean it up a bit and think that the err section could be more robust, but this is a functioning solution. 我想稍微清理它,并认为错误的部分可能更强大,但这是一个有效的解决方案。 Note that is someone else implements this, they should be aware that it is tailored to using the passport-local strategy with email instead of username. 请注意,如果其他人实现了这一点,他们应该知道它是针对使用带有电子邮件而不是用户名的本地护照策略而定制的。

I think you're looking for the register method , which will register (and hide the password) 我想你正在寻找注册方法 ,它将注册(并隐藏密码)

https://www.npmjs.com/package/passport-local-mongoose (search the register method). https://www.npmjs.com/package/passport-local-mongoose (搜索注册方法)。

app.post('/register', function(req, res) {

  // New user variable created.
  // Note: Password is not part of the new User variable; you don't want to simply store sensitive information in the database.
  var registerUser = new User({ email: req.body.email, name: req.body.name });

  // Register new user. Note the 2nd variable (password). If registration's successful (no errors), redirect.
  registerUser.register(registerUser, req.body.password, function (err, newUser){
      if(!err){
           passport.authenticate('local', req, res, function(){
                   res.redirect('/dashboard');                     
           });
       }
  });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM