0
votes

I'm developing my first app in React-native and I'm using firebase with it. It uses e-mail and password for user authentication. Before using firebase, loading raw data from code, it worked fine on Android and iOS.

When I integrated firebase with it, I couldn't make the auth work correctly on Android, for example, the buttons that are wired to firebase register, are letting the user register without throwing exceptions. But, after reloading the app to continue to the 'MainView' of the app, it would not exit the 'SplashScreen' because it is not able to retrieve information from firebase. I realized that the register was not creating the corresponding user doc in my firestore collection while in iOS, with the same code, it does!

Here's the code I'm having trouble with:

class Enter extends React.Component {

  (...) 

  signUp = () => {
    const { email, pass } = this.state;
    firebase.auth().createUserWithEmailAndPassword(email, pass).then(firebaseUser => {
      base.collection('users').doc(firebaseUser.uid).set({ email })
        .catch(error => {
          this.setState({error: error.message})
          console.log('doc requiring error: ' + error.message)
        });
    }).catch(error => {
      this.setState({error: error.message})
      console.log('account creation error: ' + error.message)
    } );
  }

  render() {
    [A button calls signUp onPress]
  }
}

am I missing something in the signUp function?

Just in case, here's the way I'm trying to retrieve the doc from App.js. It gets stuck on the SplashScreen because userReady never gets to true because the users/{uid} document was not created in the registry.

componentWillMount = async () => {
    firebase.auth().onAuthStateChanged(firebaseUser => {
      if (firebaseUser) {
        base.collection('users').doc(firebaseUser.uid)
          .onSnapshot(DocumentSnapshot => {
            this.setState({ userDocumentSnapshot: DocumentSnapshot, userReady: true })
          });
      } else {
        this.setState({ userDocumentSnapshot: null, userReady: true });
      }
    });
  }

Thanks in advance

2

2 Answers

3
votes

Yes, firestore is currently not working with react-native. Nothing happens - no error is logged or exception thrown but the database is not updated. See this issue:

firebase/firebase-js-sdk
FR: Firestore Web SDK React-Native/Expo support for Android devices · Issue #283
https://github.com/firebase/firebase-js-sdk/issues/283

Check out the suggestion by mikelehen on Nov 20, 2017. Just an interim solution but it seems to work. He says to copy

https://gist.githubusercontent.com/mikelehen/444950a35019208f3aaf18d37ab4937c/raw/85ac4cb3108d92163cb8f04fbdddcc88d4081aab/index.js
over your
node_modules/@firebase/webchannel-wrapper/dist/index.js

1
votes

This is addition to Tom's answer.

Now you dont have to do the work around suggested for firebase-js-sdk issue.

Please update firebase javascript sdk version to 4.12.1 and it will start working without any problem.