【问题标题】:Save username and password for auto connection to firebase react-native redux application保存用户名和密码以自动连接到 firebase react-native redux 应用程序
【发布时间】:2018-03-29 07:27:04
【问题描述】:

我正在为 IOS 构建一个 React-native / Redux-thunk / Firebase 应用程序。这是我的 app.js 的一部分。

  {
    this.props.data.userConnected==false?
    <View style={styles.container1}>
    <Login/>
    </View>

  :null
  }
  {
    this.props.data.userConnected==true?
    <View style={styles.container1}>
    <Connected/>
    </View>
  :null
  } 

当用户点击登录时,我正在使用 firebase.auth 函数,如果连接成功,我在 redux 存储中将变量 userConnected 设置为 true。我的问题是,大约 3 小时后,我的用户被断开连接,并且变量 userConnected 被设置为 false,因为它是初始值。

我真的不知道该怎么做,因为我在想除非应用程序被关闭并重新打开,否则无法再次初始化 redux 商店。

提前感谢您的建议。

【问题讨论】:

    标签: firebase react-native redux firebase-authentication redux-thunk


    【解决方案1】:

    您可以保存从 firebase 返回的会话令牌和刷新令牌,并将其添加到 redux-persist。每次应用程序进入后台时,您都可以检查并再次重新打开,您需要检查会话令牌是否已过期。如果是使用刷新令牌重新生成并再次保存返回值。更多信息请查看redux-persist

    【讨论】:

      【解决方案2】:

      我使用 react-native-local-storage 解决了我的问题。

      首先我用 :

      导入包
      import ls from 'react-native-local-storage';
      

      当用户首次登录时,通过 firebase.auth(email,password) 后,我会写入 iphone 的本地存储:

              ls.save('email', email)
              .then(() => {
                ls.get('email').then((data) => {console.log("get: ", data)});
                // output should be "get: Kobe Bryant"
              })
      
      
             ls.save('password', password)
              .then(() => {
                ls.get('password').then((data) => {console.log("get: ", data)});
                // output should be "get: Kobe Bryant"
              })
      

      然后,当在我的应用程序组件的 componentWillMount 函数中,我得到用户的电子邮件和密码:

         ls.get('email').then((email) => {
           if (email!=null){
              ls.get('password').then((password) => {
                console.log("password: ", password);
                console.log("email: ", email);
                this.props.initApp(3,email,'');
                this.props.initApp(4,'',password);
                this.props.initApp(1,email,password,this.props.data);
              });
            }
          });
      

      获取数据后,我正在调用我的 redux 操作,该操作调用 firebase.auth(email,password)。我们在这里,只需设置一个自动登录到我的 react native firebase 应用程序:)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-16
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 2017-08-20
        • 2019-10-24
        • 2018-04-08
        • 2014-09-28
        相关资源
        最近更新 更多