Initial commit
Next.js + Express event management app for Hope Family Church.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
|
||||
/**
|
||||
* Use inside any client component that requires authentication.
|
||||
* Returns `ready: true` only once auth has resolved and the user is confirmed.
|
||||
* While loading or unauthenticated, `ready` is false — render null to prevent flash.
|
||||
*
|
||||
* @example
|
||||
* const { ready, user, token } = useRequireAuth();
|
||||
* if (!ready) return null;
|
||||
*/
|
||||
export function useRequireAuth(redirectTo = "/login") {
|
||||
const { user, token, loading } = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!user) router.replace(redirectTo);
|
||||
}, [user, loading, router, redirectTo]);
|
||||
|
||||
const ready = !loading && !!user;
|
||||
return { ready, user, token, loading };
|
||||
}
|
||||
Reference in New Issue
Block a user