[英]Firebase currentUser and onAuthStateChanged
我正在使用本機反應開發一個應用程序,每次我在 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 import firebase from "firebase"
(刪除所有其他導入)來解決它。 顯然選擇性導入在 v8 中有一個錯誤,但至少它運行良好:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.