尽管退出,谷歌仍在重新签约

时间:2017-03-19 05:26:42

标签: javascript php google-api

我正在尝试为网站实施Google Sign-In,但我努力让用户退出网站。在将用户传回登录页面之前,我使用单独的页面来处理注销过程(它还会取消设置各种会话详细信息)。问题是,当他们点击该登录页面时,Google会将其视为已登录并将其推送到onSuccess步骤。

这是登录页面:

<!doctype html>
<html>
<head>

    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <meta name="google-signin-client_id" content="[clientIDstuffHere].apps.googleusercontent.com">

</head>
<body class="loginPage">

    <div id="loginBox">

        <?php if(isset($_GET['error'])) { ?>

            <p class="loginError"><?php echo $_GET['error'] ?></p>

        <?php } ?>

        <div id="googleSignIn"></div>

    </div>

    <script>

    function onSuccess(googleUser) {
        var id_token = googleUser.getAuthResponse().id_token;
        window.location.href = "auth.php?id_token=" + id_token;
    }

    function onFailure(error) {
        console.log(error);
    }

    function renderButton() {
        gapi.signin2.render('googleSignIn', {
            'scope': 'profile email',
            'width': 240,
            'height': 50,
            'longtitle': true,
            'theme': 'dark',
            'onsuccess': onSuccess,
            'onfailure': onFailure
        });
    } 

    </script>

    <script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>

</body>
</html>

退出页面:

<?php

session_start();
session_unset();
session_destroy();

?>
<!doctype html>
<html>
<head>

    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <meta name="google-signin-client_id" content="[clientIDstuffHere].apps.googleusercontent.com">

</head>
<body>
<script>

function loadAndExit() {
  gapi.load('auth2', function() {
    gapi.auth2.init().then(() => {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut();
        auth2.disconnect();
    });
  });
  window.location.href = "index.php";
}

</script>
<script src="https://apis.google.com/js/platform.js?onload=loadAndExit" async defer></script>
</body>
</html>

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

阅读我的评论,然后在location = 'index.php';

之后直接放置auth2.disconnect();