Firebase currentUser 和 onAuthStateChanged
Firebase currentUser and onAuthStateChanged
我正在使用 React Native 开发一个应用程序,每次我在 onAuthStateChanged 和 currentUser 中的模拟器上从 firebase 刷新应用程序时,我都会得到 null。
我读过有关等待 onAuthStateChanged 以获取状态更新的信息,但我从未这样做过,所以我想我配置错误了。
我正在使用 expo 44、react 17、firebase 9.6.5,但处于兼容模式(稍后计划完全迁移)
我第一次尝试解决方案是尝试添加持久性: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;
但是我得到一个错误:
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.create
登录我使用:
userCredential = firebase
.auth()
.signInWithEmailAndPassword(data.email, this.state.password);
我遇到了完全相同的问题。我通过将 "firebase": "^8.9.1"
添加到 package.json、运行 yarn install
并更改导入 import firebase from "firebase"
(删除所有其他导入)来解决它。显然选择性导入在 v8 中有一个错误,但至少它运行良好:)
我正在使用 React Native 开发一个应用程序,每次我在 onAuthStateChanged 和 currentUser 中的模拟器上从 firebase 刷新应用程序时,我都会得到 null。
我读过有关等待 onAuthStateChanged 以获取状态更新的信息,但我从未这样做过,所以我想我配置错误了。
我正在使用 expo 44、react 17、firebase 9.6.5,但处于兼容模式(稍后计划完全迁移)
我第一次尝试解决方案是尝试添加持久性: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;
但是我得到一个错误:
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.create
登录我使用:
userCredential = firebase
.auth()
.signInWithEmailAndPassword(data.email, this.state.password);
我遇到了完全相同的问题。我通过将 "firebase": "^8.9.1"
添加到 package.json、运行 yarn install
并更改导入 import firebase from "firebase"
(删除所有其他导入)来解决它。显然选择性导入在 v8 中有一个错误,但至少它运行良好:)