admin管理员组

文章数量:1297077

Here is my code.. its pretty simple.. just trying to allow a user to authenticate through facebook and then add it to the Firebase system! I should mention that I am using React Native, I did read that various parts of the Firebase library does not work with React Native.

const auth = firebase.auth();
const provider = firebase.auth.FacebookAuthProvider;

LoginManager.logInWithReadPermissions(['public_profile'])
.then(loginResult => {

    alert("login results")
    if (loginResult.isCancelled) {
        alert('user canceled');
        return;
    }
    AccessToken.getCurrentAccessToken()
    .then(accessTokenData => {
        const credential = firebase.auth.FacebookAuthProvider.credential(accessTokenData.accessToken);
        alert("Credential: " + credential )
        return auth.signInWithCredential(credential);
    })
    .then(credData => {
        console.log(credData);
        alert("CRED data:::" + credData)
        alert(firebase)
    })
    .catch(err => {
        console.log(err);
        alert("ERROR:::" + err)
        alert(firebase)

    });
});

The only reasoning I can really think of for this is that Firebase is undefined or it does not finish initializing.. although it seems to be.. maybe I can put in some sort of promise to ensure it has initialized. (Any ideas on how to do that?)

Although firebase seems to be initalized.. but I went through the api reference and can't really find any reason why .auth.FacebookAuthProvider.credential would not exist

Any help would be great!

Here is my code.. its pretty simple.. just trying to allow a user to authenticate through facebook and then add it to the Firebase system! I should mention that I am using React Native, I did read that various parts of the Firebase library does not work with React Native.

const auth = firebase.auth();
const provider = firebase.auth.FacebookAuthProvider;

LoginManager.logInWithReadPermissions(['public_profile'])
.then(loginResult => {

    alert("login results")
    if (loginResult.isCancelled) {
        alert('user canceled');
        return;
    }
    AccessToken.getCurrentAccessToken()
    .then(accessTokenData => {
        const credential = firebase.auth.FacebookAuthProvider.credential(accessTokenData.accessToken);
        alert("Credential: " + credential )
        return auth.signInWithCredential(credential);
    })
    .then(credData => {
        console.log(credData);
        alert("CRED data:::" + credData)
        alert(firebase)
    })
    .catch(err => {
        console.log(err);
        alert("ERROR:::" + err)
        alert(firebase)

    });
});

The only reasoning I can really think of for this is that Firebase is undefined or it does not finish initializing.. although it seems to be.. maybe I can put in some sort of promise to ensure it has initialized. (Any ideas on how to do that?)

Although firebase seems to be initalized.. but I went through the api reference and can't really find any reason why .auth.FacebookAuthProvider.credential would not exist

Any help would be great!

Share Improve this question asked Aug 29, 2016 at 11:15 Joe CaraccioJoe Caraccio 2,0354 gold badges27 silver badges42 bronze badges 6
  • Exactly which error is shown in console? – Jorgeblom Commented Aug 29, 2016 at 11:18
  • replace firebase.auth.FacebookAuthProvider.credential(..) with provider.credential(..) – Jorgeblom Commented Aug 29, 2016 at 11:20
  • Upgrade to the latest 3.3.0 version. There was a problem in the type definitions in the previous version. – bojeil Commented Aug 29, 2016 at 17:27
  • @bojeil upgraded it to 3.3.0.. I just did "npm install firebase --save" and restarted the simulator. Hopefully that was the plete process... didn't seem to work.. got the exact same error – Joe Caraccio Commented Aug 29, 2016 at 19:22
  • @Bommox did that, now the error is "Type Error: undefined is not an object (evaluating 'provider.credential') – Joe Caraccio Commented Aug 29, 2016 at 19:39
 |  Show 1 more ment

1 Answer 1

Reset to default 9

I was having this exact same issue, and I have a solution that worked for me, but I'm not sure if it will be the same for you.

I was instantiating my firebase class through a separate module, in a file called FirebaseService.js

'use-strict';

const firebase = require('firebase');
const APP_BASE = 'https://your-unique-url.firebaseapp./'

const FIREBASE_CONFIG = {
    apiKey: '[super_unique]',
    authDomain: 'your-unique-url.firebaseapp.',
    databaseURL: APP_BASE,
    storageBucket: 'your-unique-url.appspot.',
};

export const Firebase = firebase.initializeApp(FIREBASE_CONFIG);

I was then using it everywhere else in my code by importing it:

const { Firebase } = require ('./services/FirebaseService');

This appeared to be fine until I got around to implementing facebook login, and that's when I started having the same issue.

I had to import firebase in the screen that was trying to login with facebook. instead of requiring my own service, I used the npm module:

const firebase = require('firebase'); 

After that, I was able to access FacebookAuthProvider.credential. I'm really not sure why this is the case, there may have been something wrong with my use of modules

本文标签: javascriptundefined is not an object firebaseauthFacebookAuthProvidercredentialStack Overflow