Firebase Onauthstatechanged Error With React
I'm trying to check if a user is logged in or not on my React webpage using Firebase, however I keep getting an error TypeError: Cannot read property 'onAuthStateChanged' of undefi
Solution 1:
You need to include firebase package
import firebase from"firebase";
and then use auth() function of firebase
authListener() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({ user })
} else {
this.setState({ user: null })
}
})}
if you are importing firebase from another file then configure it like this
import firebase from'firebase/app';
import'firebase/auth';
exportconstinit = () => {
let config = {
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: 'xxxxxxxxxxxxx'
};
firebase.initializeApp(config);
};
exportconst firebaseAuth = firebase.auth;
Post a Comment for "Firebase Onauthstatechanged Error With React"