【问题标题】:Express.js Passport authentication automatically fails skips strategyExpress.js Passport 身份验证自动失败跳过策略
【发布时间】:2014-04-24 19:28:48
【问题描述】:

更新

我已将代码从护照本地注册内部移动到单独的处理程序,它运行良好。问题在于 Passport 和本地注册的使用,但我不知道为什么。


我有一个使用 Node.js (Express) + Passport 进行身份验证和注册的设置。这是以前使用过的标准程序,但它似乎不起作用。护照注册立即转到 failureRedirect。主要问题是代码没有命中任何 console.log() 语句,因此我假设它甚至不处理第一个请求并自动失败。知道为什么或如何调试吗?

命令日志:

POST /register 302 4ms - 58b
GET / 304 2ms
GET /css/bootstrap.css 304 1ms
GET /css/main.css 304 1ms
GET /javascript/chart.js 304 2ms
GET /javascript/bootstrap.js 304 2ms
GET /javascript/index.js 304 2ms

index.html 表单:

 <form class="form-register" name="register-form" method="post" action="/register" data-name="register Form" enctype="application/x-www-form-urlencoded">
     <div class="ARegistration" style="display:none;">
         <input type="email" id="txtOnceEmail" class="form-control" style="width:300px; display:block;" placeholder="One use Email Address" name="AEmail">
           <br />

         <input type="text" id="txtCodeName" class="form-control" style="width: 200px; display:block;" placeholder="Code Name" name="CodeName">
           <br />

         <input type="number" id="txtSecretCodeLength" value="10" class="form-control" style="width: 200px; display:block;" placeholder="Secret Code Length" name="SecretCodeLength">
           <br />

         <textarea id="txtaXInfo" class="form-control" placeholder="Any info" name="aXInfo"></textarea>

         <button class="btn btn-info btn-xs">Register</button>
      </div>
  </form>

路线:

    app.post('/register', passport.authenticate('local-signup', {
        successRedirect : '/', // redirect to the secure profile section
        failureRedirect : '/', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

护照本地注册:

// =========================================================================
// LOCAL SIGNUP ============================================================
// =========================================================================
passport.use('local-signup', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function (req, email, password, done) {
    // asynchronous
    process.nextTick(function () {
        // check if the user is already logged in
        if (!req.user) {
            console.log("step1");
            User.findOne({ $or: [{ 'valid.email': email }, { 'emailList': email }] }, function (err, user) {
                // if there are any errors, return the error
                if (err)
                    return done(err);

                // check to see if theres already a user with that email
                if (user) {
                    console.log("step2");
                    return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                } else {

                        console.log("step5");
                        newUser.anonym.CodeName = req.body.CodeName;
                        newUser.anonym.extraInfo = req.body.aXInfo;
                        newUser.emailList.push(req.body.AEmail);

                        //Generate random string
                        var rString = null;
                        var goOn = false;

                        while (!goOn)
                        {
                            console.log("step6");
                            rString = randomString(req.body.SecretCodeLength, charSet);
                            User.findOne({ 'emailList': rString }, function (err, user) {
                                // if there are any errors, return the error
                                if (err){
                                    console.log(err);
                                    return done(err);}

                                // check to see if theres already a user with that email
                                if (!user) {
                                    goOn = true;
                                }
                            });
                        }
                        console.log("step7");
                        newUser.anonym.secretCode = rString;
                        rString = null;

                        newUser.save(function (err) {
                            if (err)
                                throw err;
                            return done(null, newUser);
                        });
                    }
                // }
            });
        } else {
            console.log('test5');
        }
    });

}));

还使用 angular.js 对其进行了测试以执行帖子,但问题仍然存在。肯定是有护照的东西。

【问题讨论】:

  • 您的成功和失败重定向指向同一个位置。
  • 是的,我知道,但这不是问题。问题是它会自动失败而不调用本地注册中的任何函数
  • 你可以设置断点然后你就可以了。通常护照模块内的未处理异常将作为“失败”而不是抛出。在您调用的特定策略上放置断点,在您自己的代码之外(在护照自己的代码内)并尝试确定它。
  • 我添加了一个断点,但它从未命中,所以我认为代码实际上从未被执行,但我不明白为什么会这样。
  • 我测试了passport,加载成功。

标签: javascript node.js authentication express passport.js


【解决方案1】:

问题出在这里:

passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)

},

如果没有提供emailpassword,护照就会自动失败。我想这方面的一些文档会很有用!。

【讨论】:

    【解决方案2】:

    如果您没有在新的LocalStrategy 中识别usernameField,它将默认将用户名保存在username 中,如here 所述。

    此策略在函数之前采用可选的选项哈希,例如new LocalStrategy({/* options */, callback}).

    可用的选项有:

    • usernameField - 可选,默认为 username
    • passwordField - 可选,默认为 password

    这两个字段都定义了 POST 正文中发送到服务器的属性的名称。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 2017-05-25
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 2016-09-17
      • 2020-07-03
      • 2016-03-31
      • 2016-05-20
      相关资源
      最近更新 更多