Files
crosscode-platform/apps/events/frontend/src/hooks/useRequireAuth.ts
T
joshuaandClaude Opus 5.5 c627c61de1 Cross Events 3.0.0: sign-in through the church's Accounts
Settings -> Sign-in: check, move (everyone into Accounts with their password
hashes and authenticator apps, count/hash check, then switch) and undo for
14 days. Sign-in through auth-client with a one-time handoff to Events' own
token; staff roles stepped up to mfa; sign-out and back-channel logout;
user.updated/export/deleted; admin 2FA reset, unlock and profile changes via
Accounts; /login, /register, /forgot-password forward once moved; Events'
own sign-in endpoints answer SIGN_IN_MOVED. User.accountId, AccountsSignIn.

npm run test:accounts runs Events' code against the real Accounts app (11
tests); 5 new Jest tests. MIGRATING-TO-3.md, plan "Changes while building".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 14:10:30 +02:00

30 lines
1.0 KiB
TypeScript

"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "@/hooks/useAuth";
import { currentPage } from "@/lib/signIn";
/**
* 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;
// Back to this exact page after signing in (3.0.0: through the church's Accounts, which
// `/login` forwards to).
if (!user) router.replace(redirectTo === "/login" ? `/login?redirect=${encodeURIComponent(currentPage())}` : redirectTo);
}, [user, loading, router, redirectTo]);
const ready = !loading && !!user;
return { ready, user, token, loading };
}