Files
stack/apps/web/src/components/auth-guard.tsx
Jason Woltje d7dae21b2a feat(web): wire auth pages with BetterAuth client and route guards
Connect login/register forms to BetterAuth signIn.email() and signUp.email()
with error display, loading states, and disabled inputs during submission.
Add AuthGuard (redirects unauthenticated users to /login) on dashboard routes
and GuestGuard (redirects authenticated users to /chat) on auth routes.
Sign out in topbar now redirects to /login.

Refs #27

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 08:21:07 -05:00

35 lines
762 B
TypeScript

'use client';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
import { useSession } from '@/lib/auth-client';
interface AuthGuardProps {
children: React.ReactNode;
}
export function AuthGuard({ children }: AuthGuardProps): React.ReactElement | null {
const { data: session, isPending } = useSession();
const router = useRouter();
useEffect(() => {
if (!isPending && !session) {
router.replace('/login');
}
}, [isPending, session, router]);
if (isPending) {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="text-sm text-text-muted">Loading...</div>
</div>
);
}
if (!session) {
return null;
}
return <>{children}</>;
}