Files
vidconf/frontend/src/auth/RequireAuth.tsx

28 lines
931 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '@/auth/useAuth'
/**
* Обёртка для приватных маршрутов: пока идёт восстановление сессии —
* показывает лоадер, без сессии — редиректит на /login (сохраняя исходный
* путь, чтобы вернуться туда после входа).
*/
export function RequireAuth({ children }: { children: ReactNode }) {
const { status } = useAuth()
const location = useLocation()
if (status === 'loading') {
return (
<div className="app-loader" role="status" aria-live="polite">
Загрузка сессии
</div>
)
}
if (status === 'unauthenticated') {
return <Navigate to="/login" state={{ from: location }} replace />
}
return <>{children}</>
}