使用Google登录成功登录后如何重定向

时间:2020-06-24 07:14:56

标签: javascript html google-signin

我正在使用google的登录网站。我已经关注了this链接,因此我的代码如下所示。我试图在成功登录后将其重定向到我的索引页面,我该怎么做?

<head>
    <meta name="google-signin-client_id" content="My_client_id.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <meta name="google-signin-scope" content="profile email">
</head>

<div class="mx-auto" id="my-signin2"></div>        
<script>
    function onSuccess(googleUser) {
        console.log('Logged in as: ' + googleUser.getBasicProfile().getName());        
    }
    function onFailure(error) {
        console.log(error);
    }
    function renderButton() {
        gapi.signin2.render('my-signin2', {
            'scope': 'profile email',
            'width': 240,
            'height': 50,
            'longtitle': true,
            'theme': 'dark',
            'onsuccess': onSuccess,
            'onfailure': onFailure
        });
    }
    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;
        console.log("ID Token: " + id_token);       
      }
</script>
<script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script> ```

1 个答案:

答案 0 :(得分:1)

您可以在成功登录时提及此行- window.location.href ='#您要重定向到的网址'