[英]React-native - Firebase onAuthStateChanged not working correctly
I have not been able to persist a firebase session in my react-native application. 我无法在我的react-native应用程序中持久保存firebase会话。 This question is nearly identical to React Native - Firebase auth persistence not working but the answer provided there has not worked for me.
这个问题几乎与React Native相同- Firebase auth持久性不起作用,但提供的答案对我没有用。
Currently I log in as shown below: 目前我登录如下:
export const loginUser = ({ email, password }) => {
return (dispatch) => {
dispatch({ type: LOGIN_USER });
firebase.auth().signInWithEmailAndPassword(email, password)
.then(user => loginUserSuccess(dispatch, user))
.catch(() => loginUserFailed(dispatch))
};
};
My session is created and I have access to the user object stored on firebase. 我的会话已创建,我可以访问存储在firebase上的用户对象。
When I Cmd+R or when I close the application and reopen it my session is gone. 当我Cmd + R或当我关闭应用程序并重新打开它时,我的会话就消失了。 I have run the following in the componentWillMount() section of my app.js file:
我在app.js文件的componentWillMount()部分运行了以下命令:
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user is logged');
} else {
console.log('not logged in')
}
});
Whenever I refresh the app or close it completely and reopen I get the 'not logged in' response. 每当我刷新应用程序或完全关闭它并重新打开时,我都会收到“未登录”的响应。
I have also tried querying: 我也尝试过查询:
firebase.auth().currentUser
but get the same issue where it returns null 但在返回null的地方遇到同样的问题
What am I doing wrong? 我究竟做错了什么?
Is there a particular way to keep the session active that I am missing? 有没有一种特殊的方法可以保持会话处于活动状态而我不在用?
It's been a while since I posted this question but I thought I would post my solution. 我发布这个问题已经有一段时间,但我想我会发布我的解决方案。
As it turns out my initial set up (kind of) worked. 事实证明我的初始设置(有点)有效。 By that I mean that onAuthStateChanged was taking some time to hear back from firebase and during that time the rest of the application loaded thinking that the user was not logged in, due to that being the default state.
我的意思是onAuthStateChanged花了一些时间来回听firebase,并且在此期间加载的其他应用程序认为用户没有登录,因为这是默认状态。
I managed to get around this by moving the onAuthStateChanged logic out of the app.js and in to the initial home page in componentWillMount. 我设法通过将onAuthStateChanged逻辑移出app.js并进入componentWillMount的初始主页来解决这个问题。 which now looks like this:
现在看起来像这样:
componentWillMount() {
const { navigate } = this.props.navigation;
firebase.auth().onAuthStateChanged((user) => {
if (user) {
SplashScreen.hide()
this.setState({ loading: false })
this.props.persistedUserLoginSuccess({user, navigate})
this.props.fetchUserData();
} else {
SplashScreen.hide()
this.setState({ loading: false })
}
});
Also, I managed to better the user experience by forcing the splash screen to stay up which the auth was being queried. 此外,我设法通过强制启动屏幕以保持查询身份验证来改善用户体验。 Upon completion the splash screen was hidden and the application loaded as either a logged in application or one where the user had not yet logged in.
完成后,闪存屏幕被隐藏,应用程序作为登录的应用程序或用户尚未登录的应用程序加载。
This issue is because you are not doing Firebase.auth().signOut() after once loggedIn. 这个问题是因为您在登录后没有执行Firebase.auth()。signOut()。 Try to signOut (for testing you can always sign out in componentWillMount function) and you will be able to see the onAuthStateChanged notification.
尝试signOut(用于测试,您始终可以在componentWillMount函数中注销),您将能够看到onAuthStateChanged通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.