简体   繁体   English

React Native-Firebase Facebook身份验证

[英]React Native - Firebase Facebook Auth

Has anyone been able to succesfuly integrate Firebase Facebook Auth into React Native. 是否有人能够成功将Firebase Facebook Auth集成到React Native中。 I have been struggling with it for a few hours now and can't really figure out how to do it. 我已经为它苦苦挣扎了几个小时,还真不知道该怎么做。

Here is what I have so far: 这是我到目前为止的内容:

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

Right now it is saying that the provider variable I defined is undefined . 现在,这是说我定义的提供者变量是undefined I sit it towards the top with const provider = firebase.auth.FacebookAuthProvider; 我用const provider = firebase.auth.FacebookAuthProvider;放在顶部const provider = firebase.auth.FacebookAuthProvider;

Is there a better way to approach this? 有没有更好的方法来解决这个问题? I hope I am not alone.. Upon googling it dosen't really seem like too many other people are trying to do this.. just trying to get facebook auth on firebase to work with React Native 我希望我并不孤单。.进行谷歌搜索后,似乎并没有太多其他人在尝试这样做。.只是试图让Fireauth上的Facebook身份验证与React Native一起工作。

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

I have logged successfully with Facebook on Firebase . 我已经在Firebase上成功登录了Facebook

Here is my code. 这是我的代码。

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

Late to the party, but.. we have a complete example over in our docs here . 晚了聚会,但是..我们在这里的文档中有一个完整的示例。

Example using 'react-native-firebase' library: 使用'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}`); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM