Files
grateful-journal/src/components/ProtectedRoute.tsx
2026-04-08 11:01:53 +05:30

24 lines
516 B
TypeScript

import { type ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '../contexts/AuthContext'
import { PageLoader } from './PageLoader'
type Props = {
children: ReactNode
}
export function ProtectedRoute({ children }: Props) {
const { user, loading } = useAuth()
const location = useLocation()
if (loading) {
return <PageLoader />
}
if (!user) {
return <Navigate to="/" state={{ from: location }} replace />
}
return <>{children}</>
}