import { DEV_USER_ID } from "@env"; import { FirebaseAuthTypes } from "@react-native-firebase/auth"; import React, { createContext, useContext, useEffect, useState } from "react"; import { currentUser, handleSignOut, onAuthStateChanged } from "../auth"; import { useAuthHeader } from "../graphql/client"; interface AuthContextType { isLoggedIn: boolean; isLoading: boolean; user: FirebaseAuthTypes.User | null; logOut: () => Promise; } const AuthContext = createContext(undefined); export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const { setAuthHeader } = useAuthHeader(); const [contextUser, setContextUser] = useState( null, ); const [isLoggedIn, setIsLoggedIn] = useState(false); const [isLoading, setIsLoading] = useState(true); // this is for a LoadingContext (auth, app reloads, foreground etc) const _completeAuthentication = ( user: FirebaseAuthTypes.User, token: string, isLoggedIn: boolean, ) => { setAuthHeader({ key: "Authorization", value: token }); setContextUser(user); setIsLoggedIn(isLoggedIn); setIsLoading(false); }; const authStateChangeCallback = async (user: FirebaseAuthTypes.User) => { if (user) { const token = await user.getIdToken(); _completeAuthentication(user, token, true); } else { _completeAuthentication(undefined, undefined, false); } }; useEffect(() => { let unsubscribe = () => { console.log("Dev mode unsubscribe - really dense fn"); }; if (!DEV_USER_ID) { unsubscribe = onAuthStateChanged(authStateChangeCallback); } return unsubscribe; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { const setAuthAsync = async () => { console.log("running"); if (DEV_USER_ID) { console.log("Setting fake authorization user to: ", DEV_USER_ID); setAuthHeader({ key: "user_id", value: DEV_USER_ID }); setIsLoggedIn(true); setIsLoading(false); } }; setAuthAsync(); }, [setAuthHeader]); const logOut = async () => { await handleSignOut(); }; return ( {children} ); }; export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { throw new Error("useAuth must be used within an AuthProvider"); } return context; };