Estoy desarrollando una aplicación usando React Native, cada vez que actualizo la aplicación en el emulador en onAuthStateChanged y currentUser de firebase obtengo un valor nulo.
He leído sobre esperar onAuthStateChanged para obtener una actualización de estado, pero nunca lo hago, así que supongo que configuré algo mal.
Estoy usando expo 44, react 17, firebase 9.6.5 pero en modo compatible (planeo migrar completamente más adelante)
Mi primer intento de solución fue intentar agregar persistencia: firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
import firebase from "firebase/compat/app"; import "firebase/compat/auth"; import "firebase/compat/firestore"; import "firebase/compat/functions"; import "firebase/compat/storage"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { firebaseConfig } from "./firebase/firebaseConfig"; firebase.initializeApp(firebaseConfig); firebase.firestore(); firebase.functions(); firebase.storage(); firebase.auth(); firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL); firebase.auth().onAuthStateChanged(async (user) => { console.log("onAuthStateChanged called: ", user); if (user) { await AsyncStorage.setItem('@isLoggedIn', '1'); } else { await AsyncStorage.setItem('@isLoggedIn', '0'); } }); export default firebase;Pero me sale un error:
undefined is not an object (evaluating 'this.storage.setItem') at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:5248:0 in verifyBeforeUpdateEmail at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:5420:12 in _fromIdTokenResponse at http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:183132:41 in _set at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:1414:55 in newData.some$argument_0 at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:2060:1 in <global> at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:2047:30 in _isIOS at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:1890:4 in PersistenceUserManager.create at node_modules/@firebase/auth/dist/esm2017/index-cff4f2fd.js:1890:4 in PersistenceUserManager.createpara iniciar sesión utilizo:
userCredential = firebase .auth() .signInWithEmailAndPassword(data.email, this.state.password);Tuve exactamente el mismo problema. Lo resolví agregando "firebase": "^8.9.1" a package.json, ejecutando yarn install y cambiando la importación import firebase from "firebase" (elimine todas las demás importaciones que tenga). Aparentemente, las importaciones selectivas tienen un error en v8, pero al menos funciona bien :)