Search code examples
javascriptoauth-2.0google-oauthgoogle-api-js-client

Google Oauth popup cancellation callback


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


Solution

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