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

使用Expo与JavaScript开发React Native应用时,Firebase用户登录会话无法持久化的问题求助

Expo与JavaScript开发React Native应用时,Firebase用户登录会话无法持久化的问题求助

我现在用Expo + JavaScript开发React Native应用,集成了Firebase做身份认证功能。现在遇到个头疼的问题:用户登录成功后,关闭再打开App,或者刷新App的时候,用户会被跳回Start页面,而且user状态变成了NULL——明明登录成功后控制台还能输出正确的用户信息。

我已经排查了好几个方向,但还是没解决,想请教下大家是不是哪里漏了配置,或者是Firebase初始化的时候有竞态条件?

我已经验证/尝试过的操作

  • onAuthStateChanged()是正常工作的:登录后能返回正确的用户对象
  • 但App刷新后,这个方法返回的是null,说明会话根本没被持久化
  • 已经确保持久化配置是在Firebase Auth初始化时就设置好的(用的是initializeAuth传入配置)
  • 确认了项目里没有在持久化配置之前调用过其他获取Auth实例的方法
  • 已经安装并确认AsyncStorage能正常工作
  • 翻遍了Firebase官方文档和Expo关于持久化的指南

感觉所有配置都没问题,但会话就是存不住,是不是持久化的配置有遗漏,或者Firebase初始化的时候有什么竞态条件我没考虑到?

我的代码结构

App.js

import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import Start from './screens/Start'
import Home from './screens/Home'
import Register from './screens/Register'
import Login from './screens/Login'
import TermsAndConditions from './screens/TermsAndConditions'
import useAuth from './hooks/useAuth';

const Stack = createStackNavigator()

export default function App() {
  const { user, loading } = useAuth();
  console.log("User:", user)

  if (loading) {
    return null;
  }

  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName={user ? 'Home' : 'Start'}>
        {user ? (
          <>
            <Stack.Screen name='Home' component={Home} options={{ headerShown: false }} />
            <Stack.Screen name='TermsAndConditions' component={TermsAndConditions} options={{ headerShown: false }} />
          </>
        ) : (
          <>
            <Stack.Screen name='Start' component={Start} options={{ headerShown: false }} />
            <Stack.Screen name='Register' component={Register} options={{ headerShown: false }} />
            <Stack.Screen name='Login' component={Login} options={{ headerShown: false }} />
            <Stack.Screen name='TermsAndConditions' component={TermsAndConditions} options={{ headerShown: false }} />
          </>
        )}
      </Stack.Navigator>
    </NavigationContainer>
  );
}

firebase.js

import { initializeApp } from 'firebase/app';
import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
import AsyncStorage from "@react-native-async-storage/async-storage"

const firebaseConfig = {
  // 我的Firebase配置信息
};

export const app = initializeApp(firebaseConfig);
export const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage)
});

useAuth.js

import { useEffect, useState } from 'react';
import { onAuthStateChanged } from 'firebase/auth';
import { auth } from '../config/firebase';

export default function useAuth() {
  const [authState, setAuthState] = useState({ user: null, loading: true });

  useEffect(() => {
    const unsub = onAuthStateChanged(auth, (user) => {
      console.log("Got user:", user)
      if (user) {
        setAuthState({ user, loading: false })
      } else {
        setAuthState({ user: null, loading: false })
      }
    });

    return unsub
  }, []);

  return authState;
}

如果需要Login.js或者Register.js的代码来进一步排查,我可以再贴出来~

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 09:19:51