How to prevent any pop up Google OAuth2 (or run in background without prompt) when open new tab

1k Views Asked by At

Currently, I have a code as below to run google oauth2. It works perfectly but everytime i open a new tab a pop up will appear for login(however will auto close after few seconds). Is it possible to make it so that pop up will not appear all the time as it is distracting everytime i open a new tab.

tokenClient = google.accounts.oauth2.initTokenClient({
               client_id: CLIENT_ID,
               scope: SCOPES,
               prompt: '',
               callback: authorizeCallback
           });
tokenClient.requestAccessToken();
1

There are 1 best solutions below

0
Heiko Theißen On

When you request an access token, the following things happen:

  • The browser visits a web page from google.com.
  • As part of the HTTP request for that web page, the browser sends the google.com session cookie, which was set when the user logged on to Google.
  • (If the user is not yet logged on to Google, the loaded web page asks them to do so, after which the session cookie is set and the previous step repeated.)
  • The HTTP response issued by Google contains an access token which is valid for the currently logged on user. (Google knows this user because of the session cookie it has received.)
  • The browser is redirected to the callback URL that you specify, and the access token is injected into that URL. This allows your app to read the access token.

Google can issue an access token only in response to a visit to its web page. If you made the request to google.com from your web page, the session cookie would not be included (it counts as ). Hence a popup is necessary during every requestAccessToken(). If you want to avoid a new popup for every new tab your app opens, you must share the access token between all tabs of your app. You can achieve this, for example, by writing the access token into a cookie of your app so that it will be sent to your app server automatically after the user has logged in with Google once.

A more detailed answer would require that you share your code which triggers the Google OAuth flow.