CapacitorGoogleAuth icon indicating copy to clipboard operation
CapacitorGoogleAuth copied to clipboard

Using with Firebase?

Open tflahert opened this issue 3 years ago • 5 comments

Okay, this might come across as a dumb question, but I'm a baby with this stuff so if someone could hear me out I would really appreciate it!

I've got an Ionic app with a Firebase project, and I'm using the CapacitorGoogleAuth. How do I make it so that when a user logs in with Google, they're added to the Firebase list of users? It works fine for the email/password signup that I created, but even though everything seems to work with the Google login, it doesn't add any users to that list.

tflahert avatar Jul 01 '21 20:07 tflahert

Hi @tflahert, I can share my current implementation with you that I have in my Ionic App with Angular:

  googleAuth() {
    return GoogleAuth.signIn()
      .then((response: { authentication: Authentication }) => {
        // Create Firebase Credentials for Google
        return firebase.auth.GoogleAuthProvider.credential(
          response.authentication.idToken,
          response.authentication.accessToken
        );
      })
      .then((googleCredential) => {
        return this.loadingService.present().then(() => {
          return this.authLogin(googleCredential).then(() =>
            this.loadingService.dismiss()
          );
        });
      })
      .catch((error) => {
        this.log.error("Google-Error:", error);
      });
  }

   // Login with created credentials
  private authLogin(credential: firebase.auth.OAuthCredential) {
    return this.loadingService.present().then(() => {
      return this.fireAuth
        .signInWithCredential(credential)
        .then((userCredentials) => {
          this.customToastService.showToast(
            "auth.toasts.loginSuccess",
            "success"
          );
        })
        .catch((error) => {
          this.loadingService.dismiss();
        });
    });
  }

NLueg avatar Jul 09 '21 09:07 NLueg

Hi,

have you solved this? I use React

Richi2293 avatar Aug 30 '21 16:08 Richi2293

same problem: any idea?

ciccilleju avatar Sep 30 '21 14:09 ciccilleju

Hi,

have you solved this? I use React

I passed the id token returned from CapacitorGoogleAuth to the Firebase Google Auth method outline here: https://firebase.google.com/docs/auth/web/google-signin#expandable-2

Something like this:

 try {
        const response = await GoogleAuth.signIn();
        const idToken = response.authentication.idToken;
        const credential = GoogleAuthProvider.credential(idToken);

        // Sign in with credential from the Google user.
        return signInWithCredential(auth, credential).catch((error) => {
          // Handle Errors here.
          const errorCode = error.code;
          const errorMessage = error.message;
          // The email of the user's account used.
          const email = error.email;
          // The credential that was used.
          const credential = GoogleAuthProvider.credentialFromError(error);
          // ...
        });
           
    } catch (err) {
      console.error(err);
    }

bfeelin avatar Mar 28 '22 21:03 bfeelin

The firebase + cloud console setup is quite tricky. They have to align. Did you set up the SHA-1 fingerprint both in Firebase and your Android Client ID?

Joep-DDQ avatar Sep 13 '22 07:09 Joep-DDQ