Persist currentbase firebase with React Native

0

This function is triggered by clicking the Login button:

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

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(() => {
    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(user => {
      firebase.database().ref('/admins').once('value')
      .then(snapshot => {
        const admins = _.map(snapshot.val(), (val, uid) => {
          return { ...val, uid };
        });
        if (admins.some((item) => item.s_email === email)) {
          typeAdmin(dispatch);
        } else typeStudent(dispatch);
        loginUserSuccess(dispatch, user);
      });
    }).catch(() => loginUserFail(dispatch));
  });
};
};

Current Behavior

When you exit the application and back you need to relogar to access currentUser data.

Expected Behavior

When leaving the application keep the currentUser of the session and when nothing back has been lost.

    
asked by anonymous 27.09.2018 / 00:19

1 answer

0

I've gone through this and managed to solve it in a simple way:

isLogged() {
    if(firebase.auth().currentUser){
        //Esta logado então faça algo ...
    }
}

componentDidMount() {
    isLogged();
}

The isLogged () method must be called after the firebase has been initialized or will generate an error.

    
28.09.2018 / 18:42