wages/frontend/src/components/AuthGate.tsx
jtricerolph 2e0592eb90 Initial scaffold: wages app
Full wage cost reporting app — weekly/monthly views, rolling 12-week/12-month
history, budget management, Workforce API sync with SSE backfill, net sales
via forecasting public API, department filter, CSV export.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-23 09:03:52 +00:00

41 lines
1.1 KiB
TypeScript

import { useEffect, useState, createContext, useContext } from 'react'
import type { User } from '../types'
interface AuthCtx { user: User }
const Ctx = createContext<AuthCtx | null>(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<User | null>(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 (
<div style={{
display: 'flex', alignItems: 'center', justifyContent: 'center',
height: '100vh', color: '#6b7280',
}}>
Loading
</div>
)
}
return <Ctx.Provider value={{ user }}>{children}</Ctx.Provider>
}