7357157cb2
New pages under /ops/admin: Summary dashboard, Users (list + edit + recharge + create), LLM provider keys management, Billing ledger and usage records. Shared AuthContext (lib/auth.tsx) fetches /auth/status once and supplies user/role to RequireOps, RequireAdmin, and OpsLayout. OpsLayout shows a ⚙ 超管 nav link for admins. RequireOps now grants admin users access without an extra ops-role probe. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
31 lines
981 B
TypeScript
31 lines
981 B
TypeScript
import { createContext, useContext, useEffect, useState } from 'react';
|
|
|
|
export type AuthUser = {
|
|
id: string;
|
|
username: string;
|
|
displayName: string;
|
|
role: string;
|
|
status?: string;
|
|
};
|
|
|
|
type AuthState = { loading: boolean; user: AuthUser | null };
|
|
|
|
const AuthContext = createContext<AuthState>({ loading: true, user: null });
|
|
|
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
const [state, setState] = useState<AuthState>({ loading: true, user: null });
|
|
|
|
useEffect(() => {
|
|
void fetch('/auth/status', { credentials: 'include' })
|
|
.then((r) => r.json())
|
|
.then((auth: { authenticated: boolean; user?: AuthUser }) => {
|
|
setState({ loading: false, user: auth.authenticated ? (auth.user ?? null) : null });
|
|
})
|
|
.catch(() => setState({ loading: false, user: null }));
|
|
}, []);
|
|
|
|
return <AuthContext.Provider value={state}>{children}</AuthContext.Provider>;
|
|
}
|
|
|
|
export const useAuth = () => useContext(AuthContext);
|