Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if google auth2.signIn() window was closed by the user?

Im implementing auth using this and am currently showing a loading icon in React when a user clicks the button to sign in and the auth2 account selection/login window shows.

However if a user closes the window, there doesnt seem to be any event fired i.e the signIn() function which returns a promise never resolves, I would have thought google would return an error for this promise if the window is closed. As a result there is no way for me to stop showing the loader icon and reshow the login menu.

I was wondering if anyone had a solution for this?

like image 832
Deep Avatar asked Jan 16 '16 03:01

Deep


People also ask

Does Google OAuth use cookies?

Save questions or answers and organize your favorite content. Learn more. Show activity on this post. Google's OAuth2 documentation for Go, titled "Authenticating Users with Go" and involving a bookshelf example application, suggests storing the profile information in a cookie.


1 Answers

I try to modifiy my code that call Google OAuth 2.0 window.
You only have to add extra AJAX method that cover what is Google OAuth error result.

gapi.auth2.getAuthInstance().signIn()

Change it to this one,

gapi.auth2.getAuthInstance().signIn().then(function(response){
    //If Google OAuth 2 works fine
    console.log(response);
}, function(error){
    //If Google OAuth 2 occured error
    console.log(error);
    if(error.error === 'popup_closed_by_user'){
        alert('Oh Dude, Why you close authentication user window...!');
    }
});

That's it...

For more detail about Google OAuth 2.0 information, you can visit this link.
https://developers.google.com/api-client-library/javascript/samples/samples#authorizing-and-making-authorized-requests
Sample code on JavaScript:
https://github.com/google/google-api-javascript-client/blob/master/samples/authSample.html

like image 144
ibnɘꟻ Avatar answered Sep 29 '22 16:09

ibnɘꟻ