Firebase 与 React-Native IOS 应用程序的集成失败

Firebase integration with React-Native IOS app is failing

我正在尝试将 Firebase 服务与 React-Native 框架集成。 我正在尝试在 Udemy 中学习本教程:https://www.udemy.com/the-complete-react-native-and-redux-course/

我遵循的步骤:

  1. 使用 npm 命令下载 firebase:npm install --save react-native-firebase
  2. 在 firebase 控制台中创建了一个项目。
  3. 在 React-native 代码中粘贴了 Firebase 创建的配置。

    class 应用扩展组件 {

    componentWillMount() {
    firebase.initializeApp({
        apiKey: 'AIzaSyAMuW4adFuW_XoYvpvke_iHTvlAkuVJ7Fk',
        authDomain: 'authproj2.firebaseapp.com',
        databaseURL: 'https://authproj2.firebaseio.com',
        projectId: 'authproj2',
        storageBucket: 'authproj2.appspot.com',
        messagingSenderId: '654736603029'
      });
    
    firebase.auth().onAuthStateChanged((user) => {
        if(user)
        {
            this.setState( {loggedIn:true} );
        }
        else {
            this.setState( {loggedIn: false } );
        }
    });
    

    } }

在另一个 React 组件中,我创建了一个带有提交按钮的登录表单。

onButtonPress 函数是:

    onButtonPress () {
    const { email , password } = this.state;

    this.setState({error: '', loading: true});

    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(this.onLoginSuccess.bind(this))
    .catch(() => {
        firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(this.onLoginSuccess.bind(this))
        .catch(this.onLoginFailure.bind(this));
    });
    }

这些是 onLoginSuccess 和 OnLoginFailure 函数:

onLoginSuccess () {
this.setState( {email:'', password:'', error:'', loading:false});

}

onLoginFailure () {
this.setState( {error: "Authentication Failure!!", loading:false } );

}

但是当我点击按钮时没有任何动作发生。理想情况下,如果第一次输入电子邮件和密码,它应该创建一个新帐户,如果电子邮件和密码正确,则显示不同的页面。 但是在某个地方,Firebase 与 React-native 的集成出了问题。

您正在使用需要 firebase 包的 Web SDK,但您已经安装了 react-native-firebase 包。

如果您想继续使用您编写的代码和 firebase,那么您应该安装 firebase 包。

要解决您的问题,您应该安装 firebase 软件包

然而,为了在移动环境中正确利用 firebase,建议使用 react-native-firebase,但在这种情况下,firebase.initializeApp({ 变得毫无意义,因为移动环境中的 firebase 初始化是使用 Google-services.json使用 react-native-firebase 包时,firebase 控制台分别在 android 和 iOS 中提供和 Google-services.plist 文件。

如果您想使用 react-native-firebase,请参考 this