Authentication In React Native Using Firebase
suggest changeReplace the placeholder strings with your app's Firebase values. email and password are the credentials you collected. onLoginSuccess and onLoginFail are methods on this component. Call initializeApp once, outside the component. Calling it on every mount throws because the app already exists.
import React, { Component } from 'react';
import firebase from 'firebase';
firebase.initializeApp({
apiKey: "yourAPIKey",
authDomain: "authDomainName",
databaseURL: "yourDomainBaseURL",
projectId: "yourProjectID",
storageBucket: "storageBucketValue",
messagingSenderId: "senderIdValue"
});
export default class Login extends Component {
componentWillMount() {
firebase.auth().signInWithEmailAndPassword(email, password)
.then(this.onLoginSuccess)
.catch(() => {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(this.onLoginSuccess)
.catch(this.onLoginFail);
});
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents