'use client'; import { useSession, signOut } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import { ReactNode, useEffect } from 'react'; import { useTenant } from '../contexts/tenant-context'; export function AuthGuard({ children }: { children: ReactNode }) { const { status } = useSession(); const { activeClubId, clubs, setActiveClub, clubsLoading } = useTenant(); const router = useRouter(); useEffect(() => { if (status === 'unauthenticated') { router.push('/login'); } }, [status, router]); useEffect(() => { if (status === 'authenticated' && clubs.length > 0) { if (clubs.length === 1 && !activeClubId) { setActiveClub(clubs[0].id); } else if (clubs.length > 1 && !activeClubId) { router.push('/select-club'); } } }, [status, clubs, activeClubId, router, setActiveClub]); if (status === 'loading') { return (

Loading...

); } if (status === 'unauthenticated') { return null; } if (status === 'authenticated' && clubsLoading) { return (

Loading...

); } if (clubs.length === 0 && status === 'authenticated') { const handleSwitchAccount = () => { const keycloakLogoutUrl = `${process.env.NEXT_PUBLIC_KEYCLOAK_ISSUER || 'http://localhost:8080/realms/workclub'}/protocol/openid-connect/logout?redirect_uri=${encodeURIComponent(window.location.origin + '/login')}`; signOut({ redirect: false }).then(() => { window.location.href = keycloakLogoutUrl; }); }; return (

No Clubs Found

Contact admin to get access to a club

); } if (clubs.length > 1 && !activeClubId) { return null; } return <>{children}; }