So i am buidling a mobile app using Expo react native and firebase, i am having a issue with current user not presisting data. following are a few code snippets:
App.js:
import { AuthContextProvider } from "./context/AuthContext";
import MainNavigator from "./screens/MainNavigator";
export default function App() {
return (
<AuthContextProvider>
<MainNavigator />
</AuthContextProvider>
);
}
AuthContext.js:
import React from "react";
import { onAuthStateChanged, getAuth } from "firebase/auth";
import firebase_app from "../firebase/config";
const auth = getAuth(firebase_app);
import { Text } from "react-native";
export const AuthContext = React.createContext({});
export const useAuthContext = () => React.useContext(AuthContext);
export const AuthContextProvider = ({ children }) => {
const [user, setUser] = React.useState(null);
const [loading, setLoading] = React.useState(true);
React.useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
setUser(user);
} else {
setUser(null);
}
setLoading(false);
});
return () => unsubscribe();
}, []);
return (
<AuthContext.Provider value=>
{loading ? <Text>Loading...</Text> : children}
</AuthContext.Provider>
);
};
How i access the user:
const { user } = useAuthContext();
useEffect(() => {
console.log(user);
}, []);
This user presists after i login but as soon as i reload or reopen the app the user is null
Firebase version: 10.0.0
As per the documentation we can set the persistant to different mode and the default is local still then i set the persistant to local but didnt work.
I am expecting the user to keep its state even after the app is reloaded as the user is saved inside of async storage
Via Active questions tagged javascript - Stack Overflow https://ift.tt/WjM9uot
Comments
Post a Comment