diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index 9bdb422..dbdf8dc 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -7,14 +7,29 @@ import type { LoginUser } from "@/types/auth"; import { getLocalStorage } from "@/store/storage/secure-storage"; import { StorageKeys } from "@/store/storage/storage-keys"; +const isAuthDebugEnabled = () => + typeof window !== "undefined" && localStorage.getItem("kfpa:debugAuth") === "1"; + export const useAuth = () => { const [auth, setAuth] = useAtom(authStateAtom); useEffect(() => { + if (isAuthDebugEnabled()) { + console.log("[auth] current atom state", auth); + console.log("[auth] raw storage value", localStorage.getItem(StorageKeys.AuthState)); + } + if (auth.user != null) return; const stored = getLocalStorage(StorageKeys.AuthState) as AuthState | null; + if (isAuthDebugEnabled()) { + console.log("[auth] parsed storage value", stored); + } + if (stored?.user != null) { + if (isAuthDebugEnabled()) { + console.log("[auth] restoring user from storage", stored.user); + } setAuth(stored); } }, [auth.user, setAuth]);