mirror of
https://github.com/Noooste/garage-ui.git
synced 2026-08-11 22:16:54 +00:00
61dae6c605
Signed-off-by: Noste <83548733+Noooste@users.noreply.github.com>
30 lines
863 B
TypeScript
30 lines
863 B
TypeScript
import { Navigate, useLocation } from 'react-router-dom';
|
|
import { useAuthStore } from '@/store/auth-store';
|
|
import { LoadingSpinner } from './LoadingSpinner';
|
|
|
|
interface ProtectedRouteProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export function ProtectedRoute({ children }: ProtectedRouteProps) {
|
|
const { isAuthenticated, isLoading, config } = useAuthStore();
|
|
const location = useLocation();
|
|
|
|
if (isLoading) {
|
|
return <LoadingSpinner />;
|
|
}
|
|
|
|
// If no auth is enabled, always allow access
|
|
if (config && !config.admin.enabled && !config.oidc.enabled) {
|
|
return <>{children}</>;
|
|
}
|
|
|
|
// If not authenticated, redirect to login with return URL
|
|
if (!isAuthenticated) {
|
|
const returnUrl = encodeURIComponent(location.pathname + location.search);
|
|
return <Navigate to={`/login?returnUrl=${returnUrl}`} replace />;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|