24 lines
713 B
TypeScript
24 lines
713 B
TypeScript
// firebase/session.ts
|
|
import { getAuth, onAuthStateChanged, User } from "firebase/auth";
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
import { app } from "./config";
|
|
|
|
const auth = getAuth(app);
|
|
|
|
export const monitorAuthState = (onUserChange: (user: User | null) => void) => {
|
|
onAuthStateChanged(auth, async (user) => {
|
|
if (user) {
|
|
await AsyncStorage.setItem("user", JSON.stringify(user));
|
|
onUserChange(user);
|
|
} else {
|
|
await AsyncStorage.removeItem("user");
|
|
onUserChange(null);
|
|
}
|
|
});
|
|
};
|
|
|
|
export const getStoredUser = async (): Promise<User | null> => {
|
|
const json = await AsyncStorage.getItem("user");
|
|
return json ? JSON.parse(json) : null;
|
|
};
|