我目前正在构建一个需要Google登录的React应用程序。 当我试图构建一台以后也可以为PWA服务的服务器时,我正计划使用JWT进行身份验证。
发生的问题是我无法将我的应用程序从服务器重定向到客户端URL。
例如,假设我的客户端在localhost:8100上运行,而服务器在localhost:3100上运行。 我已经在我的react应用程序上设置了代理以将所有请求转发到/ 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以来,我还没有在服务器上初始化会话。
如果有什么方法可以打开另一个发生登录的子窗口,并且我可以将响应作为json发送,可以由父窗口读取,或者可以由本机android使用的任何其他方式/ ios应用,比这还不错,然后请分享。
答案 0 :(得分:0)
好的,所以我找出了问题所在。 在将proxy设置为react时,将changeOrigin设置为false。 它以前可以在changeOrigin设置为true的情况下工作,但现在已更改,这里是解决方法。