import { onAuthStateChanged, User } from "firebase/auth";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { auth } from "./config";

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);
    }
  });
};
