如何在页面刷新后注销用户?

Gui*_*lli 7 javascript angularjs google-signin

我正在关注Google的指南以退出用户.

考虑到gapi.auth2刷新页面后将不确定,我正在做:

if (gapi.auth2) {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut();
} else {
    gapi.load('auth2', function () {
        gapi.auth2.init({
            client_id: 'myAppID',
            cookiepolicy: 'single_host_origin'
        }).signOut();
    });
}
Run Code Online (Sandbox Code Playgroud)

但我进入uncaught exception: This method can only be invoked after the token manager is started了其他区块.

我也尝试将auth实例存储在本地存储中,但这样做会导致一些循环对象值错误,同时对其进行字符串化.

一个可行的解决方案是做一个

document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=myUrl";
Run Code Online (Sandbox Code Playgroud)

但是,除了执行不需要的重定向之外,不会仅仅记录我的应用程序用户,这会影响他所记录的所有Google服务.

有不同的方法吗?

tha*_*hpk 8

有一个更简单的方法,你只需要在调用gapi.auth2.init后调用.then

gapi.load('auth2', function () {
   var auth2 = gapi.auth2.init({
       client_id: 'myAppID',
       cookiepolicy: 'single_host_origin'
   });
   auth2.then(function(){
        // this get called right after token manager is started
        auth2.signOut();
   });
});
Run Code Online (Sandbox Code Playgroud)


Gui*_*lli 6

我不得不在我的登录页面控制器中检索GoogleAuth库的单例并设置客户端,而是必须在index.html文件中初始化它:

<script src="https://apis.google.com/js/api:client.js?onload=start" async defer></script>
<script>
    function start() {
      gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({
            client_id: 'myAppID',
            cookiepolicy: 'single_host_origin'
        });
      });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

这解决了注销问题.但是,如果登录页面被刷新,其控制器逻辑将在gapi.auth2定义之前执行,并且将点击处理程序成功附加到登录按钮是不可能的.

为了避免这种情况 - 虽然不是一个优雅的解决方案 - 我使用$ interval来等待直到gapi.auth2初始化:

waitForAuth2Initialization = $interval(function () {
    console.log("Checking...");
    if (!gapi.auth2)
        return;
    console.log("Ok, gapi.auth2 is not undefined anymore");
    var auth2 = gapi.auth2.getAuthInstance();
    // Attach signin
    auth2.attachClickHandler...

    $interval.cancel(waitForAuth2Initialization);
}, 50);
Run Code Online (Sandbox Code Playgroud)

编辑:另一种可能的解决方案是使用控制器逻辑的promise回调等待,直到解决了promise,即直到Google API完全加载并gapi.auth2准备好使用.通过以下方式实现这一目标是可行的:

<script src="https://apis.google.com/js/api:client.js?onload=start" async defer></script>
<script>
    gapiPromise = (function () {
        var deferred = $.Deferred();
        window.start = function () {
            deferred.resolve(gapi);
        };
        return deferred.promise();
    }());
    auth2Promise = gapiPromise.then(function () {
        var deferred = $.Deferred();
        gapi.load('auth2', function () {
            auth2 = gapi.auth2.init({
                client_id: 'myAppID',
                cookiepolicy: 'single_host_origin'
            }).then(function () { 
                deferred.resolve(gapi.auth2); 
            });
        });
        return deferred.promise();
    });
</script>
Run Code Online (Sandbox Code Playgroud)

然后在控制器中:

auth2Promise.then(function () {
    console.log("Ok, gapi.auth2 is not undefined anymore");
    var auth2 = gapi.auth2.getAuthInstance();
    // Attach signin
    auth2.attachClickHandler...
});
Run Code Online (Sandbox Code Playgroud)

但是,这种方法的缺点是它比使用第一个方法更慢(连接点击处理程序的时间是两倍)$interval.