Add API key authentication for external access
Some checks failed
Lint and Build / build (pull_request) Failing after 1m44s
Some checks failed
Lint and Build / build (pull_request) Failing after 1m44s
- 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>
This commit is contained in:
parent
4f9e6d2097
commit
bc4cfe607d
10 changed files with 620 additions and 48 deletions
57
contexts/ApiKeyContext.tsx
Normal file
57
contexts/ApiKeyContext.tsx
Normal file
|
@ -0,0 +1,57 @@
|
|||
'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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue