jhuleatt.com · 13d · discuss
If you're using Firebase Auth in a React app, you probably have a hook like this: import { useState, useEffect } from "react"; import { getAuth, onAuthStateChanged } from "firebase/auth"; const auth = getAuth(); export function useUser() { const [user, setUser] = useState(auth.currentUser); useEffect(() => onAuthStateChanged(auth, setUser), []); return user; } However, that hook has a big problem: it will nearly always return null immediately, resulting in a flash of unauthenticated UI before it fully initializes, even if the user is actually signed in. Why does that happen? Firebase Auth stores login data in IndexedDB, which is asynchronous. Therefore, auth.currentUser...