Ionic的Google-Plus和Firebase-Authentication插件不能一起使用

时间:2019-05-23 16:47:34

标签: ionic3 firebase-authentication cordova-plugins google-plus

Ionic(cordova)的google-plus和firebase-authentication插件不能一起使用。

我正在尝试同时使用Ionic的google-plus和firebase-authentication插件来将用户身份验证为firebase。 google-plus插件正在独立工作,我正在获取idToken和accessToken。当我添加firebase-authentication插件并运行构建时,没有任何反应。谷歌加插件没有响应,也没有错误。

下面是离子信息...

ionic(Ionic CLI):4.1.0(/home/chandra/.npm-global/lib/node_modules/ionic) 离子框架:离子角3.9.5 @ ionic / app-scripts:3.2.2

cordova(Cordova CLI):9.0.0(cordova-lib@9.0.1) Cordova平台:Android 7.1.1 Cordova插件:cordova-plugin-ionic-keyboard 2.1.3,cordova-plugin-ionic-webview 4.0.1(和其他8个插件)

Android SDK工具:26.1.1 NodeJS:v8.10.0(/ usr / bin / node) npm:6.4.0 操作系统:Linux 4.15

下面是单击“使用Google登录”按钮时调用的功能。

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);
  }
}

我打算将google-plus插件提供的idToken和accessToken传递给firebase-authentication插件,以便firebase进行身份验证。

2 个答案:

答案 0 :(得分:0)

您为什么要为Google Plus使用单独的插件(仅仅是Google登录名)? 我使用Firebase插件,而Google身份验证仅由这段代码处理

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)
    })
}

答案 1 :(得分:0)

我也正在做同样的事情,这对我有用:

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
  }
}

我希望这会有所帮助。