保存用于自动连接到firebase react-native redux应用程序的用户名和密码

时间:2018-03-29 07:27:05

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

我正在为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商店无法再次初始化。

提前感谢您的建议。

2 个答案:

答案 0 :(得分:1)

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

首先我输入pacquage:

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';

import { ContactPage } from '../pages/contact/contact';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

@NgModule({
  declarations: [
    MyApp,
    ContactPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    ContactPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

当用户第一次登录时,在传递firebase.auth(电子邮件,密码)后,我写入了iphone的本地存储:

import ls from 'react-native-local-storage';

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

        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"
        })

获取数据后,我正在调用我的redux操作,该操作调用firebase.auth(电子邮件,密码)。我们在这里,只需设置一个自动登录我的反应本机firebase应用程序:)。

答案 1 :(得分:0)

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