页面刷新后如何注销用户? [英] How to sign out a user after page refresh?

查看:92
本文介绍了页面刷新后如何注销用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 Google指南注销用户./p>

考虑到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();
    });
}

但是我在else块中得到了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";

但是,除了进行不必要的重定向之外,这不仅会影响用户登录我的应用程序,还会影响其登录的所有Google服务.

有其他方法吗?

解决方案

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

<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>

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

为了避免这种情况-尽管不是一个优雅的解决方案-我还是使用了 $间隔,直到初始化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);


编辑:另一种可能的解决方案是对控制器逻辑使用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>

然后在控制器中:

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

但是,这种方法的缺点是,与使用$interval的第一个方法相比,该方法的速度慢(附加了单击处理程序的时间是它的两倍).

I'm following Google's guide to sign out a user.

Considering that gapi.auth2 will be undefined after refreshing the page, I'm doing:

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();
    });
}

But I get uncaught exception: This method can only be invoked after the token manager is started in the else block.

I also have tried to store the auth instance in local storage but doing that led to some cyclic object value errors while stringifying it.

One posible solution is to do a

document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=myUrl";

but, instead of logging the user out only of my application, that would affect all Google's services in which he is logged, besides doing an unwanted redirection.

Is there a different approach?

解决方案

Instead of retrieving the singleton for the GoogleAuth library and setting up the client in my sign-in page controller, I had to initialize it in the index.html file:

<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>

That solved the logout problem. However, if the sign-in page was refreshed, its controller logic would be executed before the gapi.auth2 was defined and it wouldn't be posible to successfully attach the click handler to the sign-in button.

In order to avoid that - though not being an elegant solution -, I used $interval to wait until gapi.auth2 was initialized:

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);


EDIT: another possible solution is to use a promise callback for the controller logic to wait until the promise is resolved i.e. until Google API is fully loaded and gapi.auth2 is ready to use. It is posible to achieve that by doing:

<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>

And then in the controller:

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

But, the downside of this approach is that it is slower (taking twice as much time for the click handler to be attached) than the first one using $interval.

这篇关于页面刷新后如何注销用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆