You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native集成Firebase V9认证持久化时触发内部断言失败错误

React Native Firebase V9 认证持久化报错解决

问题根源

你使用了browserLocalPersistence——这是Firebase Web SDK专属的持久化方案,依赖浏览器的localStorage API,但React Native环境不存在该API,因此触发INTERNAL ASSERTION FAILED: Expected a class definition错误。

解决方案

1. 安装React Native持久化依赖

React Native环境需借助AsyncStorage实现本地存储,先安装官方适配包:

npm install @react-native-async-storage/async-storage
# 或使用yarn
yarn add @react-native-async-storage/async-storage

iOS平台需额外执行pod安装:

cd ios && pod install && cd ..

2. 替换Firebase持久化配置逻辑

使用React Native专属的认证初始化方式,在初始化阶段直接配置持久化,而非后续调用setPersistence:

修正后的完整代码

// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAuth, 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";

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "XXX",
  authDomain: "XXX",
  projectId: "XXX",
  storageBucket: "XXX",
  messagingSenderId: "XXX",
  appId: "XXX",
  measurementId: "XXX",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// 用initializeAuth替代getAuth,初始化时直接配置React Native持久化
export const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage)
});

export const db = getFirestore(app);
export default app;

关键说明

  • 使用initializeAuth而非getAuth初始化认证模块,可避免后续调用setPersistence的时序问题
  • React Native 0.60+版本会自动链接AsyncStorage,无需手动配置链接
  • 确保项目中没有重复初始化Firebase认证实例

内容的提问来源于stack exchange,提问作者franjuju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:44:58