Первоначальная версия VidConf
This commit is contained in:
27
frontend/src/auth/RequireAuth.tsx
Normal file
27
frontend/src/auth/RequireAuth.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
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}</>
|
||||
}
|
||||
Reference in New Issue
Block a user