React-native - Firebase onAuthStateChanged 无法正常工作 [英] React-native - Firebase onAuthStateChanged not working correctly

查看:70
本文介绍了React-native - Firebase onAuthStateChanged 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在我的 react-native 应用程序中保留 Firebase 会话.这个问题几乎相同React Native - Firebase 身份验证持久性不起作用但那里提供的答案对我不起作用.

I have not been able to persist a firebase session in my react-native application. This question is nearly identical to React Native - Firebase auth persistence not working but the answer provided there has not worked for me.

目前我登录如下图:

export const loginUser = ({ email, password }) => {
  return (dispatch) => {
    dispatch({ type: LOGIN_USER });

    firebase.auth().signInWithEmailAndPassword(email, password)
      .then(user => loginUserSuccess(dispatch, user))
      .catch(() => loginUserFailed(dispatch))

  };
};

我的会话已创建,我可以访问存储在 firebase 上的用户对象.

My session is created and I have access to the user object stored on firebase.

当我 Cmd+R 或当我关闭应用程序并重新打开它时,我的会话消失了.我在 app.js 文件的 componentWillMount() 部分运行了以下内容:

When I Cmd+R or when I close the application and reopen it my session is gone. I have run the following in the componentWillMount() section of my app.js file:

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.

我也试过查询:

firebase.auth().currentUser

但得到同样的问题,它返回空

but get the same issue where it returns null

我做错了什么?

是否有一种特殊的方法可以让我错过的会话保持活动状态?

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.

事实证明,我的初始设置(有点)奏效了.我的意思是 onAuthStateChanged 需要一些时间来从 firebase 收到回复,在此期间应用程序的其余部分加载认为用户未登录,因为这是默认状态.

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 逻辑从 app.js 移到 componentWillMount 中的初始主页来解决这个问题.现在看起来像这样:

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. 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.

这篇关于React-native - Firebase onAuthStateChanged 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆