Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Google sign-in with gapi.auth2 fails to start in a React app

Errorgapi.auth2.getAuthInstance().signIn()

Short answer

gapi.auth2 belongs to the Google Sign-In JavaScript library, which Google deprecated on 31 March 2023. New OAuth client IDs cannot use it, so initialisation fails. Switch to Google Identity Services: load the gsi client script and render the Google button, which replaces the signIn() call.

  • Applies to React 19, Google Identity Services
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which Google library the app loads and when the OAuth client ID was created in the Google Cloud console.

    grep -rn "apis.google.com/js\|gapi.auth2\|accounts.google.com/gsi" src/ public/
  2. 2Match what you see, then jump to its fix

    1. 1The client ID was created after July 2022 and gapi.auth2.init never succeedsThe OAuth client ID is too new for the deprecated libraryFix
    2. 2A button handler calls gapi.auth2.getAuthInstance().signIn()The code still calls signIn() on the old auth instanceFix

What does the error look like?

  • gapi.auth2.getAuthInstance().signIn()
  • New OAuth Client IDs cannot use the deprecated library.

Cause 1 of 2

The OAuth client ID is too new for the deprecated library

1Check

Google's notice says new OAuth client IDs cannot use the deprecated Google Sign-In JavaScript library. Client IDs created before 29 July 2022 can still use it.

grep -rn "gapi.auth2.init" src/

2Fix

Stop using gapi.auth2 and use Google Identity Services. Load the client library and initialise it with your client ID and a callback.

<script src="https://accounts.google.com/gsi/client" async></script>

google.accounts.id.initialize({
  client_id: "YOUR_GOOGLE_CLIENT_ID",
  callback: handleCredentialResponse
});

3Verify

The callback fires after a user signs in and receives a credential.

console.log(response.credential)

Confirmed by developers.google.com

Did this fix it?

Cause 2 of 2

The code still calls signIn() on the old auth instance

1Check

Google Identity Services has no signIn() call. Sign-in starts when the Google button is rendered and the user clicks it.

grep -rn "signIn()\|signOut()" src/

2Fix

Remove the signIn() and signOut() calls, render the button into a container, and manage the signed-in state in your own app.

<div id="buttonDiv"></div>

google.accounts.id.renderButton(
  document.getElementById("buttonDiv"),
  { theme: "outline", size: "large" }
);

3Verify

The Google button shows and clicking it starts the sign-in.

npm run dev

Confirmed by developers.google.com

Did this fix it?

How do I stop it coming back?

  • Do not start new work on gapi.auth2: Google says to migrate as soon as possible.
  • Treat the credential from sign-in as an ID token: Google's migration guide says it replaces OAuth 2.0 access tokens for sign-in.
  • Use the separate authorization migration guide if the app needs Google API scopes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.