React Native - Firebase Facebook Auth

时间:2016-08-18 14:42:33

标签: facebook firebase react-native firebase-authentication

有没有人能够成功地将Firebase Facebook Auth集成到React Native中。我一直在努力工作几个小时,而且无法真正弄清楚如何去做。

这是我到目前为止所做的:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

const FBSDK = require('react-native-fbsdk');
const {
  LoginButton,
  LoginManager,
  AccessToken
} = FBSDK;

import Button from './src/components/button';

//I had this populated with the correct stuff, just removed it for the stack overflow purpose
const firebase1 = require("firebase");
const firebaseConfig = {
  apiKey: "stuff",
  authDomain: "stuff",
  databaseURL: "stuff",
  storageBucket: "stuff",
};
const firebase = firebase1.initializeApp(firebaseConfig);

console.log("V2.5")
const auth = firebase.auth();
const provider = firebase.auth.FacebookAuthProvider;

class pack extends Component {
   render() {
    return (
      <View>        
        <LoginButton
          publishPermissions={["publish_actions"]}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("login has error: " + result.error);
              } else if (result.isCancelled) {
                alert("login is cancelled.");
              } else {
                AccessToken.getCurrentAccessToken().then(
                  (data) => {
                    alert(data.accessToken.toString())
                    console.log("lets try to sign in with provider")

                           firebase.auth().signInWithCredential(credential).catch(function(error) {
                            // Handle Errors here.
                                var errorCode = error.code;
                                var errorMessage = error.message;
                                console.log(errorMessage)
                                // ...
                              });




                  }
                )
              }
            }
          }
          onLogoutFinished={() => alert("logout.")}/>
      </View>
    );
  }

现在它说我定义的提供者变量是undefined。我用const provider = firebase.auth.FacebookAuthProvider;

坐在顶端

有没有更好的方法来解决这个问题?我希望我并不孤单..谷歌搜索它似乎真的有太多其他人试图这样做...只是想让firebase上的facebook认证与React Native一起工作

3 个答案:

答案 0 :(得分:0)

var credential = firebase.auth.FacebookAuthProvider.credential(data.accesToken)

答案 1 :(得分:0)

我已在Facebook上使用Firebase成功登录。

这是我的代码。

var credential = firebase.auth.FacebookAuthProvider.credential(data.accessToken.toString());
firebase.auth().signInWithCredential(credential).then(function(user) {
  console.log("Sign In Success", user);
}, function(error) {
  console.log("Sign In Error", error);
});

答案 2 :(得分:0)

晚会,但是......我们在文档here中有一个完整的示例。

使用'react-native-firebase'库的示例:

import { AccessToken, LoginManager } from 'react-native-fbsdk';

// ... somewhere in your login screen component
LoginManager
  .logInWithReadPermissions(['public_profile', 'email'])
  .then((result) => {
    if (result.isCancelled) {
      return Promise.resolve('cancelled');
    }
    console.log(`Login success with permissions: ${result.grantedPermissions.toString()}`);
    // get the access token
    return AccessToken.getCurrentAccessToken();
  })
  .then(data => {
    // create a new firebase credential with the token
    const credential = firebase.auth.FacebookAuthProvider.credential(data.accessToken);

    // login with credential
    return firebase.auth().signInWithCredential(credential);
  })
  .then((currentUser) => {
    if (currentUser === 'cancelled') {
      console.log('Login cancelled');
    } else {
      // now signed in
      console.warn(JSON.stringify(currentUser.toJSON()));
    }
  })
  .catch((error) => {
    console.log(`Login fail with error: ${error}`);
  });