0
votes

Ionic's (cordova) google-plus and firebase-authentication plugins not working when used together.

I am trying to use google-plus and firebase-authentication plugins of Ionic together to authenticate users into firebase. google-plus plugin is working independently and I am getting the idToken and accessToken. When I added firebase-authentication plugin, and ran the build, nothing happens. No response from google-plus plugin and no error as well.

Below is the Ionic info ...

ionic (Ionic CLI) : 4.1.0 (/home/chandra/.npm-global/lib/node_modules/ionic) Ionic Framework : ionic-angular 3.9.5 @ionic/app-scripts : 3.2.2

cordova (Cordova CLI) : 9.0.0 ([email protected]) Cordova Platforms : android 7.1.1 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.0.1, (and 8 other plugins)

Android SDK Tools : 26.1.1 NodeJS : v8.10.0 (/usr/bin/node) npm : 6.4.0 OS : Linux 4.15

Below is the function that is called when "Login with Google" button is clicked.

googlePlusLogin() {
  console.log("Trying to do Gooogle sign-in ....");
  this.gplus.login({ webClientId:  "xxx.apps.googleusercontent.com" })
  .then(res => {
    console.log("Google response: ", res);
    signinCallback(res);
  })
  .catch(err => console.error(err));

  let me = this;
  function signinCallback(authResult) {
    let res = me.firebaseAuth.signInWithGoogle(
      authResult.idToken,
      authResult.accessToken
    );
    console.log("Firebase Auth Result: ", res);
  }
}

I intend to pass the idToken and accessToken provided by google-plus plugin to firebase-authentication plugin so firebase does the authentication.

2

2 Answers

0
votes

Why would you use a separate plugin for Google Plus (is just a Google login)? I use the Firebase plugin and the Google authentication is just handled by this piece of code

import * as firebase from 'firebase';

...

googleLogin()
{
    const provider = new firebase.auth.GoogleAuthProvider();
    return this.afAuth.auth.signInWithPopup(provider).then((result) => {
        var uid = result.user.uid
        var name = result.user.displayName
        var email = result.user.email
        var photoURL = result.user.photoURL
        this.linkUser(uid, email, name, photoURL)
    })
}
0
votes

I am also using the doing the same thing and this is what worked for me:

async googleSignin(){
  try{
    const gpRes = await this.googlePlus.login({})  
      //webClientId is only required when we need the offline support, but for starting this is okay.
    const credential = this.afAuth.auth.signInWithCredential(auth.GoogleAuthProvider.credential(null, gpRes.accessToken));
      //Here also within Credential() first parameter is idToken and then accessToken, both are optional and I pass only the accessToken which is enough

      //TODO: Get the signed in user data from credential.user and update UI or change the route
  } catch (error){
     //TODO: Handle error here
  }
}

I hope this will help.