微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每个域的 Google Oauth2.0 动态网址

如何解决每个域的 Google Oauth2.0 动态网址

我目前正在开发一个应该具有 google Oauth2.0 身份验证的多域网站。我想让它在后端工作。对于每个单独的域,我都需要一个包含正确域的重定向 URL。但我似乎无法解析谷歌 OAuth 策略的自定义反馈 URL。我知道谷歌出于安全原因不允许动态网址。但我只想要“https:///googleAuthRedirect?code=”。

我计划解析请求 URL“https://universalBackend/oauth/v2/loginGoogle/”中的域

我认为通过在重定向 URL 中使用正确的域为每个请求创建自定义策略对象应该是可能的。但我似乎无法弄清楚如何将其放入代码中。

提前致谢。

我目前的passport.js conf

passport.use(
    new GoogleStrategy({
        clientID: '',clientSecret: '',},(accesstoken,refreshToken,profile,done) => {

        // Todo Check if registered in xfw,if not register,if registered then log in
        console.log('Access',profile.id);
        done();

    })
);

我当前的 authRouter.ts

oAuthRouter.get('/loginGoogle',passport.authenticate('google',{
    scope: ['profile']
}));

oAuthRouter.get('/googleRedirect',passport.authenticate('google'),(req: Request,res: Response) => {
    res.json(req.user)
});

解决方法

这就是解决方案。这样我就可以根据请求动态更改回调 URL。

oAuthRouter.get('/loginGoogle',(req: Request,res: Response,next: NextFunction) => {
    // @ts-ignore
    passport._strategies.google._callbackURL = 'http://localhost:8081/api/OauthV2/googleRedirect';

    // @ts-ignore
    console.log(passport._strategies.google._callbackURL)

    passport.authenticate('google',{ scope: ['profile'] })(req,res,next);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。