fire.js file
import firebase from 'firebase';
const firebaseConfig = {
apiKey: "AIzaSyCAXvS25TnYA3qliFTP5vNUl4hU0Ilwv2U",
authDomain: "react-firebase-auth-signup.firebaseapp.com",
projectId: "react-firebase-auth-signup",
storageBucket: "react-firebase-auth-signup.appspot.com",
messagingSenderId: "912198243014",
appId: "1:912198243014:web:bd683a011865825259fdc7"
}const fire= firebase.initialApp(firebaseConfig);
export default fire;
App.js file
import React,{Component} from 'react';
import './App.css';
import fire from './config/fire';
import Home from './Home';
import Logup from './Logup';
class App extends Component{
constructor(props){
super(props);
this.state={
user:{}
}`}`
componentDidMount(){
this.authListener();
}authListener(){
fire.auth().onAuthStateChanged((user)=>{
if(user){
this.setState({user})
}
else{
this.setState({user:null})
}
})
}
render(){
return (
<div className="App">
<h1>Hello world</h1>
{this.state.user?(<Home/>):(<Logup/>)}
console.log(firebase.default.auth)
</div>
);
}
}
export default App;
## Error ##
Compiled with problems:X
ERROR in ./src/config/fire.js 3:0-32
Module not found: Error: Package path . is not exported from package D:\myprojects\firebase projects\Signin-signup-form\signin-up\node_modules\firebase (see exports field in D:\myprojects\firebase projects\Signin-signup-form\signin-up\node_modules\firebase\package.json)
CodePudding user response:
Ensure to get the latest firebase module:
npm i firebase@latest
For v9, use this code as follows:
import { initializeApp } from "firebase/app";
import { getAuth, onAuthStateChanged } from "firebase/auth";
const firebaseConfig = {
// your-config
};
const fire = initializeApp(firebaseConfig);
export default fire;
To get the auth
, use the code below:
const auth = getAuth(fire);
onAuthStateChanged(auth, user => {
// Check for user status
});
For more information about using Firebase v9, you may check this documentation.