import { useEffect, useState, createContext, useContext } from 'react' import type { User } from '../types' interface AuthCtx { user: User } const Ctx = createContext(null) export function useAuth() { const ctx = useContext(Ctx) if (!ctx) throw new Error('useAuth must be used inside AuthGate') return ctx } export default function AuthGate({ children }: { children: React.ReactNode }) { const [user, setUser] = useState(null) useEffect(() => { fetch('/wages/api/auth/verify?app=wages', { credentials: 'include' }) .then(r => { if (!r.ok) { ;(window.top ?? window).location.href = '/login' return null } return r.json() }) .then(data => { if (data) setUser(data) }) .catch(() => { ;(window.top ?? window).location.href = '/login' }) }, []) if (!user) { return (
Loading…
) } return {children} }