Using the code below from other posts seems to suggest that this works for persisting a user (when the app is closed and launched again) through react native for firebase version 8. However using firebase v9 involves the "firebase/compat" directory which does not contain initializeAuth. Although the code runs for firebase v9 it does not persist the user. Does anyone know a solution for 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"
const defaultApp = initializeApp(config);
initializeAuth(defaultApp, {
persistence: getReactNativePersistence(AsyncStorage)
});
My sign up flow in auth context:
try {
await checkIsValidUsername(userData.username);
await checkIsValidEmail(userData.email);
await checkIsValidPassword(password);
// create in firebase
const result = await getAuth().createUserWithEmailAndPassword(userData.email, password);
if (!result || !result.user || !result.user.uid) {
dispatch({ type: 'error', payload: `Account creation failed for '${userData.email}'` });
return;
}
console.log(`Created account: ${result.user.uid} for ${result.user.email}, ${userData.username}`);
// save in the account table with the new uid and oddsPreference added in
await createUser({ ...userData, uid: result.user.uid, oddsPreference });
// Now try to login with the email and password
const userCredential = await getAuth().signInWithEmailAndPassword(userData.email, password);
const user = await getUserByUid(userCredential.user?.uid);
if (user === null) {
dispatch({ type: 'error', payload: 'Login failed!' });
} else {
// Set the user in context
dispatch({ type: 'set_user', payload: { ...user, username: user.username, email: user.email,
oddsPreference: user.oddsPreference } });
}
onSuccess();
} catch (e) {
dispatch({ type: 'error', payload: e.message });
} finally {
dispatch({ type: 'processing', payload: false });
}
};