使用React Native / Redux和Firebase保持身份验证

我有一个相当简单的React本机/ Redux应用程序,并且最近添加了Redux persist with AsyncStorage以帮助在重新加载时继承状态.

但是,我在让Firebase重新验证用户时遇到问题.我想知道是否有人有经验,因为我对Redux和Firebase都很新.为了更清楚,我希望用户登录一次,然后每次打开应用程序时都不再登录.

我的登录用户操作:

export const loginUser = ({ email,password }) => {
  return (dispatch) => {

    dispatch({ type: LOGIN_USER });

    firebase.auth().signInWithEmailAndPassword(email,password)
      .then(user => loginUserSuccess(dispatch,user))
      .catch(() => {
        firebase.auth().createuserWithEmailAndPassword(email,password)
          .then(user => loginUserSuccess(dispatch,user))
          .catch(() => loginUserFail(dispatch));
      });
  };
};

我的App.js:

class App extends Component {

    constructor() {
      super();
      this.state = {
        rehydrated: false,store
      };
    }

  componentDidMount() {

    const persistor = persistStore(
      store,{
        storage: AsyncStorage,whitelist: ['auth']
      },() => { this.setState({ rehydrated: true }); }
    );

    AppState.addEventListener('change',() => this.handleAppLoaded(AppState.currentState));

    const firebase_config = {
    apiKey: Config.FIREBASE_API_KEY,authDomain: `${Config.FIREBASE_PROJECT_NAME}.firebaseapp.com`,databaseURL: `https://${Config.FIREBASE_PROJECT_NAME}.firebaseio.com`,storageBucket: `${Config.FIREBASE_PROJECT_NAME}.appspot.com`,messagingSenderId: Config.FIREBASE_MESSAGE_ID
    };

    firebase.initializeApp(firebase_config);
  }

  componentwillUnmount() {
    AppState.removeEventListener('change',() => this.handleAppLoaded(AppState.currentState));
  }

  handleAppLoaded(state) {
    if (state === 'active') {
      store.dispatch(renewToken(store.getState()));
    }
    return null;
  }

  render() {
    if (!this.state.rehydrated)
          return null;
    this.handleAppLoaded(AppState.currentState);

    return (
      <Provider store={store}>
        <RouterWithRedux />
      </Provider>
    );
  }
}

export default App;

有人能指出我正确的方向吗?我已经尝试进行reauthUser操作,但由于我找不到重新验证用户方法而陷入困境,因为Auth对象没有保存密码(显然出于安全原因)

您使用的是网络SDK吗?如果你是,那么首先你需要使用它而不是 https://github.com/invertase/react-native-firebase

这些库用于具有本机支持的react-native项目.

如果您已经在使用它,可以在https://rnfirebase.io/docs/v3.2.x/auth/reference/auth查看API

设置所有onUserChanged,onAuthStateChangedetc.然后你的应用程序应该没有问题重新验证的东西.

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...