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

Google Oauth2 登录后将 JWT 传递给 UI 应用程序

如何解决Google Oauth2 登录后将 JWT 传递给 UI 应用程序

我创建了一个具有独立后端和前端的 MERN 应用程序。我使用 passport-google-oauth20 npm 包添加了对 Google Oauth2 登录支持

所以我在后端暴露了一个端点,如下所示:

class AccountAPIs {
    constructor() { }

    redirectToSuccess(req,res) {
        const accountServiceInst = AccountService.getInst();
        let account = req.session.passport.user;
        let filePath = path.join(__dirname + '../../../public/views/loginSuccess.html');
        let jwt = accountServiceInst.generateJWT(account);
        // how do I send this jwt to ui application
        res.sendFile(filePath);
    }

    loadMappings() {
        return {
            '/api/auth': {
                '/google': {
                    get: {
                        callbacks: [
                            passport.authenticate('google',{ scope: ['profile','email'] })
                        ]
                    },'/callback': {
                        get: {
                            callbacks: [
                                passport.authenticate('google',{ failureRedirect: '/api/auth/google/Failed' }),this.redirectToSuccess
                            ]
                        }
                    },'/success': {
                        get: {
                            callbacks: [this.successfulLogin]
                        }
                    }
                }
            }
        };
    }
}

以下是护照设置供参考:

let verifyCallback = (accesstoken,refreshToken,profile,done) => {
    const accountServiceInst = AccountService.getInst();
    return accountServiceInst.findOrCreate(profile)
        .then(account => {
            return done(null,account);
        })
        .catch(err => {
            return done(err);
        });
};

let googleStrategyInst = new GoogleStrategy({
    clientID: serverConfig.auth.google.clientId,clientSecret: serverConfig.auth.google.clientSecret,callbackURL: 'http://localhost/api/auth/google/callback'
},verifyCallback);

passport.use(googleStrategyInst);

在 UI 应用程序中,单击按钮后,我将打开一个新窗口,该窗口将打开 '/api/auth/google' 后端 API。使用 google 帐户进行身份验证后,窗口重定向'/api/auth/google/callback' 后端 API,我可以在其中生成 JWT。我不确定如何将此 JWT 传输到前端应用程序,因为它是在单独的窗口中打开的。

我知道 res.cookie('jwt',jwt) 是一种方法。请在此处建议最佳做法..

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?