Files
hemhub-web/auth/RequireAuth.tsx
Urban Modig 5bd851d380
All checks were successful
continuous-integration/drone/push Build is passing
more fix
2025-10-14 21:32:10 +02:00

19 lines
482 B
TypeScript

import type { PropsWithChildren } from 'react'
import { 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}</>
}