首页 文章

Google身份登录 . 用户登录后重定向

提问于
浏览
1

我目前正致力于使用Google的身份登录代码在我们的网站上设置自定义登录 . 我从这里开始遵循文档 . https://developers.google.com/identity/sign-in/web/

我已登录,按钮全部正常工作 . 我的问题是,我是否能够在用户成功登录后重定向?我曾尝试在API管理器中使用“授权重定向URI”,但没有运气 .

任何有关这方面的帮助或指导都会很棒 .

1 回答

  • 1

    您需要在回调函数中执行自己的重定向 . 想象一下documentation here中的代码 . 你需要做的是像这样扩展它:

    function onSignIn(googleUser) {
            // Useful data for your client-side scripts:
            var profile = googleUser.getBasicProfile();
            console.log("ID: " + profile.getId()); // Don't send this directly to your server!
            console.log('Full Name: ' + profile.getName());
            console.log('Given Name: ' + profile.getGivenName());
            console.log('Family Name: ' + profile.getFamilyName());
            console.log("Image URL: " + profile.getImageUrl());
            console.log("Email: " + profile.getEmail());
    
            // The ID token you need to pass to your backend:
            var id_token = googleUser.getAuthResponse().id_token;
            postAJAX('/server/sign-in', {id_token: id_token})
            .then(function(user) {
                // The user is now signed in on the server too
                // and the user should now have a session cookie
                // for the whole site. 
                document.location.href = '/dashboard/' + user.username
            })
            
            
          };
    

相关问题