Monday 29 May 2023

Expo Firebase Auth Persistence Not Working As Expected

I have a firebaseConfig.js that looks like this:

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";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {...};

export const app = initializeApp(firebaseConfig);

const authState = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage)
});

export const auth = authState;

export const db = getFirestore(app);

Then, when I sign in the user, it looks like this:

import { auth } from "../../firebaseConfig";
...
 signInWithEmailAndPassword(auth, email.trim(), password)
  .then(() => {
     // Handle success.
  })

Then in App.js, I have this:

import { onAuthStateChanged } from "firebase/auth";
import { auth } from "./firebaseConfig";

... 

 onAuthStateChanged(auth, user => {
  if (user) {
    ...
  } else {
    console.log("No user");
  }
});

Each time I refresh the app, I'm getting no user returned from onAuthStateChanged. It successfully detects the user after logging in or registering, but not after refreshing the app.

What am I doing wrong?



from Expo Firebase Auth Persistence Not Working As Expected

No comments:

Post a Comment