Search code examples
cordovaionic2cordova-plugins

Using Ionic2 with firebase auth


I tried to use Ionic 2 with firebase auth using this:

signInWithFacebook(): firebase.Promise<FirebaseAuthState> {
    if (this.platform.is('cordova')) {
      console.log("trying to log in using cordova...");
      Facebook.login(['email', 'public_profile']).then(res => {
        console.log("Logged in. result here");
        const facebookCredential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
        console.log(firebase.auth().signInWithCredential(facebookCredential));
        return firebase.auth().signInWithCredential(facebookCredential);
      });
    } else {
      return this.auth$.login({
        provider: AuthProviders.Facebook,
        method: AuthMethods.Popup
      });
    }
  }

This code works great in web, but doesn't work in Android (Cordova).

I debugged why the login button is not working and I found this errors:

EXCEPTION: Error in ./HomePage class HomePage - inline template:6:0 caused by: Cannot read property 'then' of undefined....

Logged in. result here

EXCEPTION: Uncaught (in promise): ReferenceError: firebase is not defined ReferenceError: firebase is not defined at file:///android_asset/www/build/main.js:42039:42 at t.invoke...

Regards the second issue (firebase is not defined) I tried to

`import firebase from 'firebase';`

but Ionic 2 build returned error.

I just used the code in the Angularfire2 auth using Ionic 2.

Anyone has any idea why it might happen?


Solution

  • You are not returning Facebook.login(). So I believe it returns error when you try to access it in your component.

      return Facebook.login(['email', 'public_profile']).then(res => {