trading-platform-frontend-v2/src/components/ProtectedRoute.tsx
rckrdmrd 737303d177 Migración desde trading-platform/apps/frontend - Estándar multi-repo v2
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-16 08:32:49 -06:00

58 lines
1.6 KiB
TypeScript

/**
* Protected Route Component
* Redirects to login if not authenticated
*/
import { FC, ReactNode } from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { useIsAuthenticated, useAuthLoading } from '../modules/auth';
interface ProtectedRouteProps {
children: ReactNode;
}
export const ProtectedRoute: FC<ProtectedRouteProps> = ({ children }) => {
const isAuthenticated = useIsAuthenticated();
const isLoading = useAuthLoading();
const location = useLocation();
// Show loading spinner while checking auth state - STC Theme (Gold/Black)
if (isLoading) {
return (
<div className="min-h-screen bg-primary-900 flex items-center justify-center">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-gold"></div>
</div>
);
}
// Redirect to login if not authenticated
if (!isAuthenticated) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
return <>{children}</>;
};
/**
* Public Route Component
* Redirects to dashboard if already authenticated
*/
interface PublicRouteProps {
children: ReactNode;
}
export const PublicRoute: FC<PublicRouteProps> = ({ children }) => {
const isAuthenticated = useIsAuthenticated();
const location = useLocation();
// Get the intended destination from state, default to dashboard
const from = (location.state as { from?: Location })?.from?.pathname || '/dashboard';
// Redirect to dashboard if already authenticated
if (isAuthenticated) {
return <Navigate to={from} replace />;
}
return <>{children}</>;
};