I am trying to integrate the Firebase service with React-Native framework. I am trying to follow this tutorial in Udemy: https://www.udemy.com/the-complete-react-native-and-redux-course/
Steps I followed:
- Downloaded firebase using npm command: npm install --save react-native-firebase
- Created a project in the firebase console.
Pasted the config created by Firebase in the React-native code.
class App extends Component {
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 } ); } });
} }
And in a different React component I am creating a a Login Form which has a Submit button.
The onButtonPress function is :
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));
});
}
These are the onLoginSuccess and OnLoginFailure functions:
onLoginSuccess () {
this.setState( {email:'', password:'', error:'', loading:false});
}
onLoginFailure () {
this.setState( {error: "Authentication Failure!!", loading:false } );
}
But when I click on the button no action takes place. Ideally it should create an a new account if the email and password are entered for the first time and show a different page if the email and password are correct. But somewhere the integration of Firebase with React-native is going wrong.