Files
spsays-training/components/auth/use-signed-in-page.ts
T
2026-10-15 21:09:37 +05:30

36 lines
1.0 KiB
TypeScript

"use client";
/**
* useSignedInPage — for a page that only makes sense signed in.
*
* Signed-out visitors get the sign-in modal over the page; dismissing it
* takes them home. This is the same gate /notifications, /chats and /u/me
* each wire up by hand. Returns the user (null until signed in) so the page
* can hold its content back until then.
*/
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "@/components/auth/auth-provider";
import { useAuthGate } from "@/components/auth/auth-gate";
export function useSignedInPage() {
const { user, loading } = useAuth();
const { requireAuth } = useAuthGate();
const router = useRouter();
useEffect(() => {
if (loading || user) return;
let cancelled = false;
(async () => {
const ok = await requireAuth();
if (!ok && !cancelled) router.replace("/");
})();
return () => {
cancelled = true;
};
}, [loading, user, requireAuth, router]);
return { user, loading };
}