Home > Enterprise >  Best way to persist firebase login using expo go
Best way to persist firebase login using expo go

Time:03-22

I am trying to make an app in React Native using Expo Go. have successfully gotten google sign in working and it's all good. Unfortunately, the user's login does NOT persist between app relaunches. At first, I thought it was an async storage problem, but I can save items in storage just fine between relaunches.

What data would I need to save manually to be able to log the user back in whenever the app launches?

I would prefer to persist the data manually, as all the methods that are supposed to work, do not work for me. Methods I have tried:

  • Calling setPersistence(getAuth(), browserLocalPersistence) gives me an error, see this post for the exact error that I am getting.
  • Using onAuthStateChanged also does not work for me, in fact, that is the main way I know when to log in. The user object is just always null, and never changes from null until the user logs in again (which happens on every app launch).
    useEffect(() => getAuth().onAuthStateChanged(value => {
      if (value) {
        console.log("User is signed in");
      }  else {
        console.log("User is signed out");
        // navigation.navigate("login")
      }
    }), [])
    

(This code will always print user is signed out on app relaunch.)
At this point, I think it would just be easier if I could just save the user's credentials myself and then log them in silently on app launch. I am having a hard time finding code to do this.

CodePudding user response:

You need to use SecureStore to presist the data in the device. Something like LocalStorage in web, SharedPreference in Android and NSUserDefaults in iOS.

Example:

// save your thing
async function save(key, value) {
  await SecureStore.setItemAsync(key, value); 
}

// get your thing
async function getValueFor(key) {
  let result = await SecureStore.getItemAsync(key);
  if (result) {
    alert("           
  • Related