|
@@ -26,9 +26,17 @@ async function checkSession(): Promise<{ state: GateState; auth: AuthUser | null
|
|
|
if (!res.ok) return { state: 'demo', auth: null }
|
|
if (!res.ok) return { state: 'demo', auth: null }
|
|
|
// La preview estática responde HTML con 200 (SPA fallback): no es sesión
|
|
// La preview estática responde HTML con 200 (SPA fallback): no es sesión
|
|
|
if (!(res.headers.get('content-type') ?? '').includes('application/json')) return { state: 'demo', auth: null }
|
|
if (!(res.headers.get('content-type') ?? '').includes('application/json')) return { state: 'demo', auth: null }
|
|
|
- const data = (await res.json()) as { user?: string; role?: 'admin' | 'user'; language?: 'auto' | 'es' | 'en' }
|
|
|
|
|
|
|
+ const data = (await res.json()) as {
|
|
|
|
|
+ user?: string
|
|
|
|
|
+ role?: 'admin' | 'user'
|
|
|
|
|
+ language?: 'auto' | 'es' | 'en'
|
|
|
|
|
+ displayName?: string
|
|
|
|
|
+ }
|
|
|
if (!data?.user) return { state: 'demo', auth: null }
|
|
if (!data?.user) return { state: 'demo', auth: null }
|
|
|
- return { state: 'authed', auth: { user: data.user, role: data.role ?? 'user', language: data.language ?? 'auto' } }
|
|
|
|
|
|
|
+ return {
|
|
|
|
|
+ state: 'authed',
|
|
|
|
|
+ auth: { user: data.user, role: data.role ?? 'user', language: data.language ?? 'auto', displayName: data.displayName ?? '' },
|
|
|
|
|
+ }
|
|
|
} catch {
|
|
} catch {
|
|
|
return { state: 'demo', auth: null }
|
|
return { state: 'demo', auth: null }
|
|
|
}
|
|
}
|
|
@@ -82,6 +90,18 @@ export function AuthGate({ children }: { children: React.ReactNode }) {
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
}, [])
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
+ // Refetch bajo demanda (SPEC-65 D65-5): tras cambiar displayName/idioma, la
|
|
|
|
|
+ // página emite 'netpulse-auth-refresh' y el contexto se actualiza sin reload.
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ const onRefresh = () => {
|
|
|
|
|
+ void checkSession().then(({ state: s, auth: a }) => {
|
|
|
|
|
+ if (s === 'authed') setAuth(a)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ window.addEventListener('netpulse-auth-refresh', onRefresh)
|
|
|
|
|
+ return () => window.removeEventListener('netpulse-auth-refresh', onRefresh)
|
|
|
|
|
+ }, [])
|
|
|
|
|
+
|
|
|
if (state === 'loading') return <GateSkeleton />
|
|
if (state === 'loading') return <GateSkeleton />
|
|
|
if (state === 'login') return <Navigate to="/login" replace />
|
|
if (state === 'login') return <Navigate to="/login" replace />
|
|
|
return <AuthProvider auth={auth}>{children}</AuthProvider>
|
|
return <AuthProvider auth={auth}>{children}</AuthProvider>
|