Authenticated.tsx 676 B

1234567891011121314151617181920212223
  1. import './Authenticated.scss'
  2. import LoadingIndicator from './LoadingIndicator'
  3. import type { PropsWithChildren } from 'react'
  4. import { useSession } from '@/hooks'
  5. import { Navigate, useLocation } from 'react-router-dom'
  6. export default function Authenticated({ children }: PropsWithChildren) {
  7. const location = useLocation()
  8. const session = useSession()
  9. if (!session.ready) return (
  10. <div className="authenticating">
  11. <LoadingIndicator>Checking session</LoadingIndicator>
  12. </div>
  13. )
  14. if (session.loggedIn) return children
  15. const redirect = location.pathname
  16. return (
  17. <Navigate to={{ pathname: '/login', search: `redirect=${redirect}` }} />
  18. )
  19. }