Что я проверил /попробовал: < /p>
[*]
Код: Выделить всё
onAuthStateChanged()[*] После обновления он запускает с помощью нулевого, означающего сеанс не сохранялся
[*] Убедитесь, что setpersistence () называется сразу после Getauth (app)
[*] no ale to getauth ()
[*] no altauth () нет. SET
[*] Подтверждено AsyncStorage установлен и работает
Прочитайте все связанные документы Firebase и руководства по стойкости выставки инициализируется?
Код: Выделить всё
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 } = useAuth();
console.log('User:', user);
if (user) {
return (
);
} else {
return (
);
}
}
< /code>
firebase.js:
import { initializeApp } from 'firebase/app';
import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
import AsyncStorage from "@react-native-async-storage/async-storage"
const firebaseConfig = {
.... my config here ....
};
export const app = initializeApp(firebaseConfig);
export const auth = initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage)
});
< /code>
Useauth.js:
import { useEffect, useState } from 'react';
import { onAuthStateChanged } from 'firebase/auth';
import { auth } from '../config/firebase';
export default function useAuth() {
const [user, setUser] = useState(null);
useEffect(() => {
const unsub = onAuthStateChanged(auth, (user) => {
console.log("Got User:", user);
if (user) {
setUser(user)
} else {
setUser(null)
}
});
return unsub
}, []);
return { user };
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... t-in-react
Мобильная версия