mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-08-09 19:04:47 +00:00
24 lines
684 B
TypeScript
24 lines
684 B
TypeScript
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
import { useRouter, usePathname } from 'next/navigation';
|
|
import { ConfigurationService } from '@/lib/api/services/configuration';
|
|
|
|
export function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
|
|
useEffect(() => {
|
|
const publicPaths = ['/', '/login', '/unauthorized'];
|
|
const isPublicPath = publicPaths.some((path) =>
|
|
path === '/' ? pathname === '/' : pathname.startsWith(path)
|
|
);
|
|
|
|
if (!isPublicPath && !ConfigurationService.isTokenValid()) {
|
|
router.push('/login');
|
|
}
|
|
}, [router, pathname]);
|
|
|
|
return <>{children}</>;
|
|
}
|