Files
akademiawodociagowca/frontend/src/academy/RequireStudent.tsx
T
2026-09-02 08:21:27 +02:00

33 lines
1.0 KiB
TypeScript

import { Navigate, useLocation } from 'react-router-dom'
import type { ReactNode } from 'react'
import { Spinner } from '../components/ui'
import { STUDENT_ROUTES } from '../lib/routes'
import { useStudentAuth } from './StudentAuthContext'
/**
* Trasa kursanta. Niezalogowany trafia na logowanie Akademii.
*
* Zabezpieczenie interfejsu, nie zastepuje regul po stronie backendu.
* Backend i tak odrzuci zadanie bez tokenu kursanta, a token panelu nie
* otworzy /api/academy/**.
*/
export function RequireStudent({ children }: { children: ReactNode }) {
const { student, loading } = useStudentAuth()
const location = useLocation()
if (loading) {
return (
<div className="flex min-h-screen items-center justify-center">
<Spinner label="Sprawdzanie sesji..." />
</div>
)
}
if (!student) {
// sciezka razem z parametrami: odtwarzacz trzyma biezaca lekcje w ?lekcja=
return <Navigate to={STUDENT_ROUTES.login} replace state={{ from: location.pathname + location.search }} />
}
return <>{children}</>
}