Reactjs firebase在React中的持久性,每个页面上的用户日志都会重新加载
我在Firebase身份验证状态持久性方面遇到了一个非常严重的问题,当使用单页web应用程序时,我能够访问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.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;
});