5

When using Google Identity Services (GSI) I can display a popup to ask users to connect with their Google account. This is pretty well documented and it works well with this code:

const client = window.google.accounts.oauth2.initCodeClient({
  client_id: 'CLIENT_ID',
  scope: 'SCOPE',
  ux_mode: 'popup',
  callback: async (response) => {
    console.log('Response Google', response);
  },
});
client.requestCode();

However, I wish to do something if the user close the popup. I can't find anything in the documentation and in examples online. I tried intermediate_iframe_close_callback and native_callback, but neither are called when closing the popup.

So, is it possible ? How can I do it ?

Thanks

  • 2
    There is a nice solution to this where you add a focus event listener on the window: https://stackoverflow.com/a/73485415/4875396 – Robert Desmond Aug 29 '22 at 16:40

2 Answers2

4

I think the callback name is "error_callback". You can find details at: Handle Errors

const client = google.accounts.oauth2.initCodeClient({
  client_id: 'YOUR_GOOGLE_CLIENT_ID',
  scope: 'https://www.googleapis.com/auth/calendar.readonly',
  ux_mode: 'popup',
  callback: myCallback,
  error_callback: myErrorCallback  // You can do something when popup window closed
});
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Fred
  • 116
  • 1
  • 3
1

It appears that this is not working for the current version of GSI.

It did work for the old gapi version and if the popup were to be closed you would get a response with the error: {error: "popup_closed_by_user"}. As referenced in this answer: Google SSO login error: "popup_closed_by_user"

Hopefully adding the #google-oauth tag will allow someone at Google to see this and hopefully update this script.