firebase auth delayed on refresh

后端 未结 4 1613
醉话见心
醉话见心 2021-02-05 20:50

Hard refreshes on my SPA React/Firebase application does not maintain auth state on immediate execution of a function. I have a workaround, but it\'s sketchy.

My react

4条回答
  •  不要未来只要你来
    2021-02-05 21:47

    This worked for me try to wrap your main app in an if else statement depending on the state of firebase.auth .

     constructor() {
        super();
        this.state={
        user:{},
        stateChanged:false
        };
      } 
    componentDidMount(){
    fire.auth().onAuthStateChanged((user)=>{
          this.setState({stateChanged:true})
        });
    }
    render() { 
        if(this.state.stateChanged==false){
          return(
            
    Loading
    ) } else{ return(
    your code goes here...
    ) } }

提交回复
热议问题