[英]Detect if user closes popup when using gapi.auth.authorize
Using the gapi.auth.authorize function, the user can close the popup without clicking any option (no accept or deny button). 使用gapi.auth.authorize函数,用户可以关闭弹出窗口而不单击任何选项(不接受或拒绝按钮)。 When this case happens, my callback function doesn't fire, so that I can't handle this case. 当这种情况发生时,我的回调函数不会触发,所以我无法处理这种情况。 What's the way to resolve this scenario? 解决这种情况的方法是什么?
Thanks. 谢谢。
They don't appear to mention it in any documentation, but gapi.auth.authorize()
returns the popup Window
. 他们似乎没有在任何文档中提及它,但gapi.auth.authorize()
返回弹出Window
。 So you can save the returned Window
and set an interval or timeout to check Window.closed
. 因此,您可以保存返回的Window
并设置间隔或超时以检查Window.closed
。
So you the auth function from Google returns promise , not a window. 所以你从谷歌的auth功能返回承诺 ,而不是窗口。 But then you can wrap original window into the function, which will set interval, to check if opened window closed already. 但是你可以将原始窗口包装到函数中,该函数将设置间隔,以检查打开的窗口是否已经关闭。
// variable to store our deferred object
var authDefer = null;
function auth() {
// right before the auth call, wrap window.open
wrap();
// Call auth
authDefer = window.gapi.auth.authorize({
client_id: ...,
scope: ...,
immediate: ...
}).then(
// onSuccess,
// onReject,
// onNotify
);
}
function wrap() {
(function(wrapped) {
window.open = function() {
// re-assign the original window.open after one usage
window.open = wrapped;
var win = wrapped.apply(this, arguments);
var i = setInterval(function() {
if (win.closed) {
clearInterval(i);
if (authDefer) {
authDefer.cancel();
}
}
}, 100);
return win;
};
})(window.open);
}
Taken from one of the thread on Google forums. 摘自谷歌论坛上的一个主题。 Really works. 真的有用。
This question has been around for a while, but when I looked into the issue (I want to show a spinner while the google authentication window is open, and hide it if the user decides not to authenticate), and found that gapi is throwing an error popup_closed_by_user
. 这个问题已经存在了一段时间,但是当我调查这个问题时(我希望在谷歌身份验证窗口打开时显示一个微调器,如果用户决定不进行身份验证则隐藏它),并发现gapi正在抛出一个问题错误popup_closed_by_user
。 There is a two-second delay (which is kind of long, Facebook's is instant) before it is thrown, but it does work. 在投掷之前有两秒钟的延迟(有点长,Facebook是即时的),但确实有效。 Hooray, Google! 万岁,谷歌!
Some sample code (angular 1.x), prompting
is the attribute to show the spinner: 一些示例代码(angular 1.x), prompting
是显示微调器的属性:
_google_obj.prompting = true;
gapi.auth2.getAuthInstance().signIn().then(function(googleResponse){
var token = googleResponse.getAuthResponse().id_token;
SVC_exec_.post('/q/goog', 1000, { token: token }, 'signing you in through Google', function (response) {
if (response.code === 'ok') {
// update the UI
}
_google_obj.prompting = false;
});
},
function(error){
$timeout(function () {
console.log('user probably closed the google popup window: '+error);
_google_obj.prompting = false;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.