无法从服务器重定向到客户端 URL
Unable to redirect to client URL from server
我目前正在构建一个需要 google 登录的 React 应用程序。
由于我正在尝试构建一个稍后也可以为 PWA 提供服务的服务器,因此我计划使用 JWT 进行身份验证。
出现的问题是我无法将我的应用程序从服务器重定向到客户端 URL。
例如,假设我的客户端是 localhost:8100 上的 运行,服务器是 localhost:3100 上的 运行。
我已经在我的反应应用程序上设置了代理,以将所有请求转发到 /auth/* 到服务器。
身份验证完成后,当我尝试使用 res.redirect("/handleAuth/$token") 从我的服务器重定向到客户端时,我被重定向到 localhost:3000/handleAuth/$token 而不是 localhost:8100/handleAuth/$token
这是服务器代码
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,所以我还没有在服务器上初始化会话。
如果有任何方法我可以打开另一个 child window 进行登录并且我可以将响应发送为 json 可以由 parent window,或者其他原生android/ios应用也可以使用的比这更体面的方法,请分享。
好的,所以我发现了问题所在。
在 react 中设置代理时,将 changeOrigin 设置为 false。
它以前可以在将 changeOrigin 设置为 true 的情况下工作,但现在它已更改,这是修复方法。
我目前正在构建一个需要 google 登录的 React 应用程序。 由于我正在尝试构建一个稍后也可以为 PWA 提供服务的服务器,因此我计划使用 JWT 进行身份验证。
出现的问题是我无法将我的应用程序从服务器重定向到客户端 URL。
例如,假设我的客户端是 localhost:8100 上的 运行,服务器是 localhost:3100 上的 运行。 我已经在我的反应应用程序上设置了代理,以将所有请求转发到 /auth/* 到服务器。 身份验证完成后,当我尝试使用 res.redirect("/handleAuth/$token") 从我的服务器重定向到客户端时,我被重定向到 localhost:3000/handleAuth/$token 而不是 localhost:8100/handleAuth/$token
这是服务器代码
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,所以我还没有在服务器上初始化会话。
如果有任何方法我可以打开另一个 child window 进行登录并且我可以将响应发送为 json 可以由 parent window,或者其他原生android/ios应用也可以使用的比这更体面的方法,请分享。
好的,所以我发现了问题所在。 在 react 中设置代理时,将 changeOrigin 设置为 false。 它以前可以在将 changeOrigin 设置为 true 的情况下工作,但现在它已更改,这是修复方法。