react-firehooks
Version:
Lightweight dependency-free collection of React hooks for Firebase
16 lines (15 loc) • 800 B
JavaScript
import { onAuthStateChanged } from "firebase/auth";
import { useListen } from "../internal/useListen.js";
import { LoadingState } from "../internal/useLoadingValue.js";
const onChange = (stableAuth, next, error) => onAuthStateChanged(stableAuth, next, (e) => error(e));
/**
* Returns and updates the currently authenticated user
* @param auth Firebase Auth instance
* @returns User, loading state, and error
* - value: User; `undefined` if user is currently being fetched, or an error occurred
* - loading: `true` while fetching the user; `false` if the user was fetched successfully or an error occurred
* - error: `undefined` if no error occurred
*/
export function useAuthState(auth) {
return useListen(auth, onChange, () => true, auth.currentUser ? auth.currentUser : LoadingState);
}