【问题标题】:why is my google OAuth using passport.js not executing the callback function?为什么我的google OAuth 使用passport.js 没有执行回调函数?
【发布时间】:2021-05-22 10:26:29
【问题描述】:

我正在尝试使用带有 passport.js 的 google + API 为我的网站启用注册和登录访问。

router.get("/api/v1.0/authentication/register/google",restrict({ unregistered: true }) ,passport.authenticate("google", {scope:["profile", "email"]}));

try{
    router.get("/api/v1.0/google/callback", restrict({ unregistered: true }), passport.authenticate("google", {failureRedirect: "/"}), (req,res)=>{
         console.log("success");
        res.end("signed in");
        // res.end("success")

    
    });}
catch(error){
    console.error(error);
}

这是我的 passport.js 文件,我在其中输入了我的 clientID 和密码。

    passport.use(new GoogleStrategy({
      clientID:     "CLIENT_ID",
    clientSecret: "CLIENT_SECRET",
    callbackURL: "http://localhost:3000/api/v1.0/google/callback",
    passReqToCallback   : true
  },
  function(req, accessToken, refreshToken, profile, done) {
   //register user here
   if(req.body===null){
       console.log("yes");
       return done(null, false);
   }
   else{
   console.log("the profile is:",profile);
   }
  }

));

每次我使用谷歌OAuth登录,但认证完成后不执行回调函数。它显示发生了错误。

【问题讨论】:

  • 错误是什么?您可以将其添加到问题中吗?
  • 它是网站失败时产生的错误信息。我正在使用一个名为 reeve (github.com/peterjoseph/Reeve) 的开源软件,因此它会自动生成一个错误,当请求或功能未完成或执行时会显示该错误。它只是说 {"status":500,"message":"看起来我们的系统出了点问题。不要惊慌!在我们的工程师调查期间,请随时与我们联系并提供以下代码以获取更多详细信息。代码:1he57cfbkklcavtpb"},我无法调试它导致错误也没有出现在控制台上

标签: javascript node.js express google-api passport.js


【解决方案1】:

你没有给它任何地方重定向到你的尝试中,所以它总是会失败

router.get("/api/v1.0/authentication/register/google",restrict({ unregistered: true }) ,passport.authenticate("google", {scope:["profile", "email"]}));
    
    try{
        router.get("/api/v1.0/google/callback", restrict({ unregistered: true }), passport.authenticate("google", {failureRedirect: "/"}), (req,res)=>{
            console.log("success");
            res.end("signed in");
            // res.end("success")
            res.redirect('/');
        
        });}
    catch(error){
        console.error(error);
    }

【讨论】:

  • 我确实尝试过重定向,但它不起作用。它陷入无限循环并说数据丢失。
猜你喜欢
  • 2021-01-06
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
相关资源
最近更新 更多