30 lines
789 B
TypeScript
30 lines
789 B
TypeScript
/**
|
|||
|
|
* ProtectedRoute — renders children when authenticated; redirects to /login otherwise.
|
||
|
|
*
|
||
|
|
* Shows nothing while the session is still loading to avoid flash-of-login.
|
||
|
|
* T07 can replace the loading placeholder with a proper spinner/skeleton.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import type { ReactNode } from 'react'
|
||
|
|
import { Navigate } from 'react-router-dom'
|
||
|
|
import { useSession } from './SessionProvider'
|
||
|
|
|
||
|
|
interface ProtectedRouteProps {
|
||
|
|
children: ReactNode
|
||
|
|
}
|
||
|
|
|
||
|
|
export function ProtectedRoute({ children }: ProtectedRouteProps) {
|
||
|
|
const { status } = useSession()
|
||
|
|
|
||
|
|
if (status === 'loading') {
|
||
|
|
// Render nothing while we check the session — avoids a flash to /login.
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
|
||
|
|
if (status === 'unauthenticated') {
|
||
|
|
return <Navigate to="/login" replace />
|
||
|
|
}
|
||
|
|
|
||
|
|
return <>{children}</>
|
||
|
|
}
|