Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs firebase在React中的持久性,每个页面上的用户日志都会重新加载_Reactjs_Firebase_Firebase Authentication - Fatal编程技术网

Reactjs firebase在React中的持久性,每个页面上的用户日志都会重新加载

Reactjs firebase在React中的持久性,每个页面上的用户日志都会重新加载,reactjs,firebase,firebase-authentication,Reactjs,Firebase,Firebase Authentication,我在Firebase身份验证状态持久性方面遇到了一个非常严重的问题,当使用单页web应用程序时,我能够访问Firebase.auth(),直到显式注销,现在每次重新加载时,我都没有定义,用户是loggedOut,我将此用于导航: <HashRouter> <Switch> <Route exact path="/dashboard" name="Dashboard" component={DefaultLayout} />

我在Firebase身份验证状态持久性方面遇到了一个非常严重的问题,当使用单页web应用程序时,我能够访问
Firebase.auth()
,直到显式注销,现在每次重新加载时,我都没有定义,用户是loggedOut,我将此用于导航:

  <HashRouter>
    <Switch>
      <Route exact path="/dashboard" name="Dashboard" component={DefaultLayout} />
      {this.state.loggedOut ? <Route path="/" name="Home" component={Login} /> : null }
      {this.state.loggedIn ? <Route path="/" name="Home" component={DefaultLayout} /> : null }          
    </Switch>
  </HashRouter>
第一次尝试时,dashboard上的一切都正常工作,为了查看发生了什么,我在dashboard.js上使用了这个

   componentDidMount() {
     const { currentUser } = fire.auth();
     this.setState({ fullname:  currentUser ? currentUser.displayName : 'TEST'  })
   }

  render() {
    return (
      <p>{this.state.fullname}</p>

   ...
componentDidMount(){
const{currentUser}=fire.auth();
this.setState({fullname:currentUser?currentUser.displayName:'TEST'})
}
render(){
返回(
{this.state.fullname}

...
当我重新加载页面时,用户是loggedOut并获得fullname:TEST


我不知道为什么会发生这种情况。请有人帮助

您必须在登录时指定持久性

从文档:

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(function() {
    // Existing and future Auth states are now persisted in the current
    // session only. Closing the window would clear any existing state even
    // if a user forgets to sign out.
    // ...
    // New sign-in will be persisted with session persistence.
    return firebase.auth().signInWithEmailAndPassword(email, password);
  })
  .catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
  });

好的,我认为这很明显。只需将您的
firebase.auth().signIn….
放在
中。然后
firebase.auth().setPersistence()上的
函数
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(function() {
    // Existing and future Auth states are now persisted in the current
    // session only. Closing the window would clear any existing state even
    // if a user forgets to sign out.
    // ...
    // New sign-in will be persisted with session persistence.
    return firebase.auth().signInWithEmailAndPassword(email, password);
  })
  .catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
  });