【问题标题】:Node.js/ mysql passport-facebook authenticateNode.js/ mysql 护照-facebook 认证
【发布时间】:2019-12-07 12:36:31
【问题描述】:

我通过接受来自 facebook 的电子邮件地址来设置使用护照-facebook 身份验证的登录页面。但是当出现重复条目​​时,它会重定向到成功页面。请帮助我

function(accessToken, refreshToken, profile, done) {
    process.nextTick(function() {
        if (fbconfig.use_database === true) {
            const qr = ("SELECT * from tbl_user where email ='" + profile.emails[0].value + "';");
            pool.query(qr, (err, rows) => {
                if (err) {
                    throw err;
                }
                if (rows && rows.length === 0) {
                    console.log(profile.emails[0].value);
                    console.log(profile.id);
                    console.log(profile.displayName);
                    var dt = dateTime.create();
                    var d = dt.format('Y-m-d H:M:S');
                    console.log("Creating profile");
                    let sql = ("INSERT into tbl_user(fb_id,token,name,email,cur_date) VALUES('" + profile.id + "','" + accessToken + "','" + profile.displayName + "','" + profile.emails[0].value + "','" + d + "');");
                    pool.query(sql, function(err, result) {
                        if (err) {
                            throw err;

                        }
                        console.log("fb inserted");


                    });



                } else {

                    console.log("Error Email already exist");

                }
            });

        }
        return done(null, profile);
    });
}));
router.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
router.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/sample', failureRedirect: '/', failureFlash: true }),
function(req, res) {
    pool.query(qr, (err, rows) => {
        if (err) {
            throw err;
        }
        if (rows && rows.length > 0) {
            res.render('/');
        }


    })


}
);

这是我的代码,我找不到解决方案

【问题讨论】:

    标签: node.js express passport-facebook


    【解决方案1】:

    每当发生错误时尝试完成(错误)。

    例如,试试

    if (err) {
        throw err;
    }
    

    if(err) {
        done(err);
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:
          function(accessToken, refreshToken, profile, done) {
      process.nextTick(function() {
          if (fbconfig.use_database === true) {
              const qr = ("SELECT * from tbl_user where email ='" + profile.emails[0].value + "';");
              pool.query(qr, (err, rows) => {
                  if (err) {
                      throw err;
                  }
                  if (rows && rows.length === 0) {
                      console.log(profile.emails[0].value);
                      console.log(profile.id);
                      console.log(profile.displayName);
                      var dt = dateTime.create();
                      var d = dt.format('Y-m-d H:M:S');
                      console.log("Creating profile");
                      let sql = ("INSERT into tbl_user(fb_id,token,name,email,cur_date) VALUES('" + profile.id + "','" + accessToken + "','" + profile.displayName + "','" + profile.emails[0].value + "','" + d + "');");
                      pool.query(sql, function(err, result) {
                          if (err) {
                              throw err;
      
                          }
                          console.log("fb inserted");
      
      
                      });
      return done(null, true);
      
                  } else {
      
                      console.log("Error Email already exist");
      
      
      return done(null, false);
      
                  }
              });
      
          }
      
      });
      }));
      router.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
      router.get('/auth/facebook/callback',
      passport.authenticate('facebook', { successRedirect: '/sample', failureRedirect: '/', failureFlash: true }),
      function(req, res) {
      res.redirect('/sample')   
      }
      
      );
      

      【讨论】:

        猜你喜欢
        • 2013-09-04
        • 2017-09-13
        • 1970-01-01
        • 2017-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-05
        • 1970-01-01
        相关资源
        最近更新 更多