【问题标题】:Passport.authenticate loops foreverPassport.authenticate 永远循环
【发布时间】:2022-02-11 08:32:48
【问题描述】:

我正在使用 Passport.js 通过 GoogleStrategy 将我的 Node 应用程序连接到 Google+ API。 我将 auth/api 路由从我的前端 (localhost:3000) 调用到我的服务器 (localhost:5150)。 我的代码成功到达路由并调用了 passport.authenticate("google", { scope: ["email"] }) 方法,但没有执行任何操作。我的前端只是无休止地等待 Google 回调,但它永远不会到来。

为什么会这样?

我的代码:

export const authenticateUsingGoogle = (req, res) => {
  console.log(`authenticating using google`);
    passport.authenticate("google", { scope: ["email", "openid", "profile "] });
  console.log(`authenticated using google`);
};

我在 Stackoverflow 上看到过两个类似的问题,但他们没有答案:

passport.authenticate() loads forever

Passport.authenticate() gets stucks

【问题讨论】:

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


    【解决方案1】:

    passport.authenticate 方法返回一个用于 express 的中间件。您需要将其用作快速路由器的参数。

    例子:

    app.post('/login/password',
      passport.authenticate("google", { scope: ["email", "openid", "profile "] }),
      function(req, res) {
        // Your actual code.
      });
    

    您可以找到更多信息here

    【讨论】:

    • 我包含了它。还是不行。
    • 您是否尝试过使用其他策略(例如基本身份验证)进行设置?找到问题的根本原因、谷歌策略或护照模块本身很重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2017-10-12
    • 2021-11-18
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多