【问题标题】:Unable to redirect to client URL from server无法从服务器重定向到客户端 URL
【发布时间】:2020-07-02 17:28:16
【问题描述】:

我目前正在构建一个需要 google 登录的 React 应用程序。 由于我正在尝试构建一个以后也可以为 PWA 提供服务的服务器,因此我计划使用 JWT 进行身份验证。

发生的问题是我无法将我的应用程序从服务器重定向到客户端 URL。

例如,假设我的客户端在 localhost:8100 上运行,而服务器在 localhost:3100 上运行。 我已经在我的反应应用程序上设置了代理,以将所有对 /auth/* 的请求转发到服务器。 身份验证完成后,当我尝试使用 res.redirect("/handleAuth/$token") 从服务器重定向到客户端时,我被重定向到 localhost:3000/handleAuth/$token 而不是 localhost:8100/handleAuth /$令牌

这是服务器代码

const passport = require("passport");

passport.use(
    new GoogleStrategy(
      {
        ...keys.google,
        callbackURL: "/auth/google/callback",
        proxy: true
      },
      async (accessToken, refreshToken, { _json }, done) => {
        done(null, _json);
      }
    )
  );

app.get(
    "/auth/google",
    passport.authenticate("google", {
      session: false,
      scope: ["openid", "profile", "email"]
    })
  );

  app.get(
    "/auth/google/callback",
    passport.authenticate("google", { session: false }),
    (req, res) => {
      const token= generateToken(req.user);

      // Redirecting to serverUrl/handleAuth/${token} instead of clientURL/handleAuth/${token}
      res.redirect(`/handleAuth/${token}`)
    }
  );

由于我使用的是 JWT,我还没有在服务器上初始化会话。

如果有任何方法可以打开另一个发生登录的子窗口,我可以将响应作为父窗口可以读取的 json 发送,或者本机 android 也可以使用的任何其他方式比这更体面的/ios应用,那就分享一下吧。

【问题讨论】:

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


【解决方案1】:

好的,所以我发现了问题所在。 在 react 中设置代理时,将 changeOrigin 设置为 false。 它以前在 changeOrigin 设置为 true 的情况下工作,但现在它已更改,这里是修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2023-03-08
    • 2018-11-06
    • 1970-01-01
    相关资源
    最近更新 更多