Spaces:
Paused
Paused
File size: 1,032 Bytes
ec17af3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { authApi } from "@/lib/api";
import { useAuthStore } from "@/store/useAuthStore";
export const useRequireAuth = () => {
const router = useRouter();
const bootstrap = useAuthStore((state) => state.bootstrap);
const bootstrapped = useAuthStore((state) => state.bootstrapped);
const userEmail = useAuthStore((state) => state.userEmail);
useEffect(() => {
let active = true;
bootstrap().then(async (authenticated) => {
if (!active || authenticated) return;
try {
const status = await authApi.setupStatus();
if (!active) return;
if (status.setup_required) {
router.replace("/setup");
return;
}
} catch {
// Fall back to login page
}
if (active) {
router.replace("/login");
}
});
return () => {
active = false;
};
}, [bootstrap, router]);
return {
ready: Boolean(bootstrapped && userEmail),
};
};
|