2026-03-03 19:59:14 +01:00
|
|
|
'use client';
|
|
|
|
|
|
2026-03-09 14:21:03 +01:00
|
|
|
import { useSession, signOut } from 'next-auth/react';
|
2026-03-03 19:59:14 +01:00
|
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
|
import { ReactNode, useEffect } from 'react';
|
|
|
|
|
import { useTenant } from '../contexts/tenant-context';
|
|
|
|
|
|
|
|
|
|
export function AuthGuard({ children }: { children: ReactNode }) {
|
2026-03-18 09:08:45 +01:00
|
|
|
const { data, status } = useSession();
|
2026-03-06 08:01:09 +01:00
|
|
|
const { activeClubId, clubs, setActiveClub, clubsLoading } = useTenant();
|
2026-03-03 19:59:14 +01:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (status === 'unauthenticated') {
|
|
|
|
|
router.push('/login');
|
|
|
|
|
}
|
|
|
|
|
}, [status, router]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-03-18 09:08:45 +01:00
|
|
|
if (status === 'authenticated') {
|
|
|
|
|
const isAdmin = (data?.user as any)?.isAdmin;
|
|
|
|
|
|
|
|
|
|
// Admin routing
|
|
|
|
|
if (isAdmin) {
|
|
|
|
|
if (!window.location.pathname.startsWith('/admin')) {
|
|
|
|
|
router.push('/admin/clubs');
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normal user routing
|
|
|
|
|
if (clubs.length > 0) {
|
|
|
|
|
if (clubs.length === 1 && !activeClubId) {
|
|
|
|
|
setActiveClub(clubs[0].id);
|
|
|
|
|
} else if (clubs.length > 1 && !activeClubId) {
|
|
|
|
|
router.push('/select-club');
|
|
|
|
|
}
|
2026-03-03 19:59:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-18 09:08:45 +01:00
|
|
|
}, [status, clubs, activeClubId, router, setActiveClub, data]);
|
2026-03-03 19:59:14 +01:00
|
|
|
|
|
|
|
|
if (status === 'loading') {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-center min-h-screen">
|
|
|
|
|
<p>Loading...</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status === 'unauthenticated') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 08:01:09 +01:00
|
|
|
if (status === 'authenticated' && clubsLoading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-center min-h-screen">
|
|
|
|
|
<p>Loading...</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 09:08:45 +01:00
|
|
|
const isAdmin = (data?.user as any)?.isAdmin;
|
|
|
|
|
if (clubs.length === 0 && status === 'authenticated' && !isAdmin) {
|
2026-03-09 14:21:03 +01:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-03 19:59:14 +01:00
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col items-center justify-center min-h-screen gap-4">
|
|
|
|
|
<h2 className="text-2xl font-bold">No Clubs Found</h2>
|
|
|
|
|
<p>Contact admin to get access to a club</p>
|
2026-03-09 14:21:03 +01:00
|
|
|
<button
|
|
|
|
|
onClick={handleSwitchAccount}
|
|
|
|
|
className="mt-4 px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-800 rounded-md border border-gray-300 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Use different credentials
|
|
|
|
|
</button>
|
2026-03-03 19:59:14 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 09:08:45 +01:00
|
|
|
if (clubs.length > 1 && !activeClubId && !isAdmin) {
|
2026-03-03 19:59:14 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|