【发布时间】: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应用,那就分享一下吧。
【问题讨论】:
-
我建议你参考这个文档passportjs.org/docs/downloads/htmlstackoverflow 中已经有很多答案stackoverflow.com/search?q=GoogleStrategy
标签: node.js reactjs express authentication passport.js