Files
hemhub-web/auth/RequireAuth.tsx

18 lines
454 B
TypeScript

import { PropsWithChildren, useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import { useAuth } from './AuthProvider'
export function RequireAuth({ children }: PropsWithChildren) {
const { isAuthenticated, signIn } = useAuth()
const loc = useLocation()
useEffect(() => {
if (!isAuthenticated) void signIn(loc.pathname + loc.search)
}, [isAuthenticated, signIn, loc])
if (!isAuthenticated) return null
return <>{children}</>
}