Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase user is logged out on refresh of App - React Native Expo

Context

I am aware that this question has been asked multiple times across stackoverflow, and believe me I have looked through them and tried as many approaches as I could find.

Problem

I have a React Native - Expo app that uses Firebase authentication. The user is logged out on every app refresh (infuriating). So, I added a listener as many other answers suggest. This was added on my login page. After every refresh, "No User" is logged in my console.

useEffect(() => {
    const unsubscribe = firebase.auth().onAuthStateChanged(user => {
      if (user) {
        navigation.replace('Main');
        console.log('User logged in!');
      }
      else {
        console.log("No user");
      }
    });

    return unsubscribe;
  }, []);

As I am using V9.6.10, I have also tried the modular approach offered as an answer to a similar issue here.

These answers provided in other issues seem to work for people, but none have worked for me so far. If more information is needed to solve the issue, I am happy to provide it.

This is one of the final issues of an app that I've been working on for 9 months and has remained my nemesis for a while, help would be greatly appreciated!

like image 710
JSim Avatar asked Oct 27 '25 02:10

JSim


1 Answers

solution is given in this post: Best way to persist firebase login using expo go

import { initializeApp } from "firebase/app"
import { initializeAuth } from "firebase/auth"
import { getReactNativePersistence } from "firebase/auth/react-native"
import AsyncStorage from "@react-native-async-storage/async-storage"

const defaultApp = initializeApp(config);
initializeAuth(defaultApp, {
  persistence: getReactNativePersistence(AsyncStorage)
});
like image 87
Mauro Russo Avatar answered Oct 29 '25 16:10

Mauro Russo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!