0
votes

Once I execute

firebase.auth().signInWithEmailAndPassword(email, password).then((user) => {
    // here I save user to redux and set loggedIn to true
});

I am logged in. What next? My suggestions... After login I go to main flow of App and in App.js I execute onAuthStateChanged(). And if I still get user I am still logged in, if not I set loggedIn key to false and redirect user to Login screen. Next, I am using Redux persist to save loggedIn key between different launches of App.

But when (except I log out by myself) I'll be logged out? What period of life of auth session (if it exists) on firebase side? How it works?

1
"What period of life of auth session (if it exists) on firebase side?" Firebase Authentication sessions don't have an expiration interval. They last until the user signs out, or until there is another compelling event (e.g. a password change elsewhere) that requires them to reauthenticate. See stackoverflow.com/questions/37907096/… - Frank van Puffelen
Ok, got it. I am logged in always by default. I check it by executing onAuthStateChnged in App.js, correct? So that each time app loading the user see spinner while the app connecting to firebase server to check if the user still logged in and if there were no any actions that forced user to log out. - John Smith
That sounds correct. But it's hard to be certain without seeing the code. Note that persisting the auth state on React Native has historically been tricky, so if you have a problem, be sure to post back with the minimal code that reproduces where you are stuck. - Frank van Puffelen

1 Answers

1
votes

.onAuthStateChanged() drives the sign-in state of your app. That's how it works.

firebase.auth().onAuthStateChanged(function (user) {
    if(user){
      //unhide non-public elements
    } else {
      //hide public elements
    }
});

You then use Firebase Security Rules to control who has access to what information.