Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleAuth.signIn: undefined is not an object (evaluating 'gapi.auth2.getAuthInstance') #336

Open
Linksku opened this issue Feb 6, 2024 · 1 comment

Comments

@Linksku
Copy link

Linksku commented Feb 6, 2024

If people try to sign in before the Google API file loads, it'll show the error in the title. I can consistently repro this error by throttling my network speed.

I tried to fix this by adding await this.gapiLoaded; to the beginning of signIn(). However, this.gapiLoaded is undefined, even though initialize() ran already.

In this screenshot, gapiResolve is defined, which means initialize() ran already. I don't know why this.gapiLoaded and this.options are undefined. Seems like something weird with Capacitor's plugin proxy?

Screenshot 2024-02-06 014510

The GoogleAuth object in initialize() vs in signIn():

Screenshot 2024-02-06 020512

@Monomachus
Copy link
Contributor

Monomachus commented Mar 15, 2024

I've resolved it like this...

In my code I've created a method where I try and check if the window.gapiResolve property is defined or not.
If defined then it's probably loaded correctly and vice versa...

async waitForGapiResolve() {
    return new Promise((resolve, reject) => {
      const intervalId = setInterval(() => {
        if (window['gapiResolve']) {
          clearInterval(intervalId);
          resolve(true); // gapiResolve is available
        } else {
          console.log('waiting for 100ms more...');
        }
      }, 100); // Check every 100ms
  
      // Set a maximum timeout 
      setTimeout(() => {
        clearInterval(intervalId);
        reject(new Error('Gapi resolve timed out')); 
      }, 3000); // Example 3-second timeout
    });
  }

Then the code will run it before running the signIn method

await this.waitForGapiResolve();
const googleUser = await GoogleAuth.signIn();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants