简体   繁体   中英

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). 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 . So you can save the returned Window and set an interval or timeout to check Window.closed .

So you the auth function from Google returns promise , not a window. 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.

External link to Source

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 . There is a two-second delay (which is kind of long, Facebook's is instant) before it is thrown, but it does work. Hooray, Google!

Some sample code (angular 1.x), prompting is the attribute to show the spinner:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM