Reactjs 在React中,每次我刷新页面时,Firebase auth user都为null
我被以下问题困住了。我问了几个与同一问题相关的问题和答案,但没有一个对我有用 我正在使用firebase.auth().onAuthStateChanged检查用户是否已登录,但每当我刷新页面时,我总是得到用户null PFB代码Reactjs 在React中,每次我刷新页面时,Firebase auth user都为null,reactjs,firebase,firebase-authentication,Reactjs,Firebase,Firebase Authentication,我被以下问题困住了。我问了几个与同一问题相关的问题和答案,但没有一个对我有用 我正在使用firebase.auth().onAuthStateChanged检查用户是否已登录,但每当我刷新页面时,我总是得到用户null PFB代码 import React, { Component } from 'react'; import Header from './Header'; import Home from './Home/components/Home'; import Footer from
import React, { Component } from 'react';
import Header from './Header';
import Home from './Home/components/Home';
import Footer from './Footer/components/Footer';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { BrowserRouter as Router, Route, Redirect, withRouter, Switch } from 'react-router-dom';
import { auth } from '../firebase/auth';
import Dashboard from './Dashboard/components/Dashboard';
import * as actions from '../actions/index';
import {Map, List, fromJS} from 'immutable';
class App extends Component {
constructor(props){
super(props);
this.state = {
loading: true,
authenticated: false,
user: null
}
}
componentDidMount() {
auth.onAuthStateChanged(user => {
//user is null every time I refresh the page
if (user) {
this.props.userLoading(false);
this.props.authUser(true);
this.props.storeUser(user.providerData);
this.props.history.push('/dasboard');
} else {
this.props.userLoading(false);
this.props.authUser(false);
this.props.history.push('/');
}
});
}
componentWillUnMount(){
this.unSubscribe();
}
render(){
const { authenticated, loading } = this.state;
return(
<div>
<Header />
<Router>
<div>
<Route path='/' exact component={Home}/>
<Route path='/dashboard' component={Dashboard} />
</div>
</Router>
<Footer />
</div>
)
}
}
const mapStateToProps = state => {
const user = state.getIn(['user', 'data'], List());
const authenticate = state.getIn(['user', 'auth'], false);
const userLoading = state.getIn(['user', 'loading'], false);
return {
user,
authenticate,
userLoading
};
}
const actionsToProps = {
storeUser: actions.storeUser,
authUser: actions.authUser,
userLoading: actions.userLoading
}
export default withRouter(connect(mapStateToProps, actionsToProps)(App))
我使用的是firebase 5.8.3版
我尝试过很多方法,比如使用async await、setTimeout,但都不起作用。您似乎正在auth.js中注销:
firebase.auth().signOut()代码>
Firebase将注销操作排队,并在Auth实例就绪时运行该操作。您的Firebase版本是什么?在v4.0.0之后,事件发生了一些更改。你可以在bennygenel网站上读到,谢谢你的评论。我正在使用5.8.3Try版本在授权下添加您的域Domains@ManzurulHoqueRumi你是说firebase?是的,在firebase。谢谢你的回答。我会在某个时候检查并更新
import firebase from './firebase';
export const auth = firebase.auth();
export const logout = firebase.auth().signOut();
export const githubProvider = firebase.firebase_.auth.GithubAuthProvider();
export const twitterProvider = new firebase.firebase_.auth.TwitterAuthProvider();
export const facebookProvider = new firebase.firebase_.auth.FacebookAuthProvider();