我正在开发MERN应用,并在客户端使用Firebase身份验证。
当用户首次登录时,该用户的信息(来自firebase)将保存在数据库中。
export const startLogIn = () =>{
return firebase.auth().signInWithRedirect(googleAuthProvider)
.then(result => {
if(result.additionalUserInfo.isNewUser){
console.log(result.user);
axios.post(`http://localhost:5000/api/user`, result.user)
.then(() => console.log(newUser.data))
.catch(err => console.log(err))
}else{
console.log('existing user');
}
});
};
在app.js中,我正在从数据库中获取用户数据
firebase.auth().onAuthStateChanged((user) => {
if(user){
store
.dispatch(usersFetchData(`http://localhost:5000/api/user/${user.uid}`));
store.dispatch(startSetPost()).then(() => {
renderApp();
if(history.location.pathname === '/'){
history.push('/dashboard');
}
});
}
else{
console.log('log out');
store.dispatch(logout());
renderApp();
history.push('/');
}
});
但是该策略不起作用,因为axios请求是异步的,所以在 发布请求完成后,app.js分解功能开始运行。