obs-ss-plugin-webui/contexts/ApiKeyContext.tsx
Decobus bc4cfe607d
Some checks failed
Lint and Build / build (pull_request) Failing after 1m44s
Add API key authentication for external access
- Create API key context for managing authentication state
- Add dedicated settings page for API key management
- Move performance metrics to dedicated page in navigation
- Update middleware to support URL parameter fallback
- Enhance UI with proper glass morphism styling
- Add Solarized color utilities to CSS
- Improve spacing and padding throughout UI components
- Remove manual bullet points from list items

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-26 00:19:16 -04:00

57 lines
No EOL
1.4 KiB
TypeScript

'use client';
import React, { createContext, useContext, useState, useEffect } from 'react';
interface ApiKeyContextType {
apiKey: string | null;
setApiKey: (key: string) => void;
clearApiKey: () => void;
isAuthenticated: boolean;
}
const ApiKeyContext = createContext<ApiKeyContextType | undefined>(undefined);
export function ApiKeyProvider({ children }: { children: React.ReactNode }) {
const [apiKey, setApiKeyState] = useState<string | null>(null);
const [isLoaded, setIsLoaded] = useState(false);
// Load API key from localStorage on mount
useEffect(() => {
const stored = localStorage.getItem('obs-api-key');
if (stored) {
setApiKeyState(stored);
}
setIsLoaded(true);
}, []);
const setApiKey = (key: string) => {
localStorage.setItem('obs-api-key', key);
setApiKeyState(key);
};
const clearApiKey = () => {
localStorage.removeItem('obs-api-key');
setApiKeyState(null);
};
const isAuthenticated = Boolean(apiKey);
// Don't render children until we've loaded the API key from storage
if (!isLoaded) {
return <div>Loading...</div>;
}
return (
<ApiKeyContext.Provider value={{ apiKey, setApiKey, clearApiKey, isAuthenticated }}>
{children}
</ApiKeyContext.Provider>
);
}
export function useApiKey() {
const context = useContext(ApiKeyContext);
if (context === undefined) {
throw new Error('useApiKey must be used within an ApiKeyProvider');
}
return context;
}