Files
OpenMAIC/components/access-code-guard.tsx
T
131af9c25a fix(access-code): refetch server providers after access code is accepted (#1081)
ServerProvidersInit fetches /api/server-providers on mount, which on an
ACCESS_CODE-gated deployment happens before any access cookie exists. The
middleware answers 401 and fetchServerProviders returns early, leaving the
settings store on its blank defaults. AccessCodeModal calls onSuccess without
reloading and nothing else calls fetchServerProviders, so every
server-configured provider reads as unconfigured for the whole session until
the user manually reloads.

Re-run the fetch from AccessCodeGuard's onSuccess, once the cookie makes the
request authorizable.

Co-authored-by: thanos-openclaw <thanos@openclaw.local>
Co-authored-by: wyuc <wang-yc24@mails.tsinghua.edu.cn>
2026-08-12 12:16:32 +08:00

61 lines
1.9 KiB
TypeScript

'use client';
import { useEffect, useState, ReactNode } from 'react';
import { AccessCodeModal } from '@/components/access-code-modal';
import { useSettingsStore } from '@/lib/store/settings';
export function AccessCodeGuard({ children }: { children: ReactNode }) {
const [status, setStatus] = useState<{
enabled: boolean;
authenticated: boolean;
loading: boolean;
}>({ enabled: false, authenticated: false, loading: true });
useEffect(() => {
let cancelled = false;
fetch('/api/access-code/status')
.then((res) => res.json())
.then((data) => {
if (!cancelled) {
setStatus({
enabled: data.enabled,
authenticated: data.authenticated,
loading: false,
});
}
})
.catch(() => {
if (!cancelled) {
// Default to requiring auth on error — safer than silently disabling
setStatus({ enabled: true, authenticated: false, loading: false });
}
});
return () => {
cancelled = true;
};
}, []);
const needsAuth = !status.loading && status.enabled && !status.authenticated;
return (
<>
{needsAuth && (
<AccessCodeModal
open={true}
onSuccess={() => {
setStatus((s) => ({ ...s, authenticated: true }));
// ServerProvidersInit runs on mount, which on an ACCESS_CODE-gated
// deployment is before any access cookie exists: the middleware
// answers 401 and the store silently keeps its blank defaults.
// Nothing re-fetches afterwards, so every server-configured
// provider reads as unconfigured until a manual reload. Re-fetch
// now that the request will be authorized.
void useSettingsStore.getState().fetchServerProviders();
}}
/>
)}
{children}
</>
);
}