"use client" import React, { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Alert, AlertDescription } from '@/components/ui/alert' import { Badge } from '@/components/ui/badge' import { authApi } from '@/lib/api' import { useAuth } from '@/lib/auth-context' interface ApiKey { id: string name: string key: string scopes: string[] created_at: string last_used?: string expires_at?: string active: boolean } export function ApiKeyManager() { useAuth() const [apiKeys, setApiKeys] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [showCreateForm, setShowCreateForm] = useState(false) const [newKeyName, setNewKeyName] = useState('') const [newKeyScopes, setNewKeyScopes] = useState([]) const [createdKey, setCreatedKey] = useState(null) useEffect(() => { loadApiKeys() }, []) const loadApiKeys = async () => { try { setLoading(true) const data = await authApi.getApiKeys() setApiKeys(data.keys || []) setError(null) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load API keys') } finally { setLoading(false) } } const handleCreateKey = async (e: React.FormEvent) => { e.preventDefault() if (!newKeyName.trim()) { setError('Key name is required') return } try { const data = await authApi.createApiKey(newKeyName.trim(), newKeyScopes) setCreatedKey(data.key) setApiKeys(prev => [data.key, ...prev]) setNewKeyName('') setNewKeyScopes([]) setShowCreateForm(false) setError(null) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to create API key') } } const handleDeleteKey = async (keyId: string) => { if (!confirm('Are you sure you want to delete this API key? This action cannot be undone.')) { return } try { await authApi.deleteApiKey(keyId) setApiKeys(prev => prev.filter(key => key.id !== keyId)) setError(null) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to delete API key') } } const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text).then(() => { // You could add a toast notification here }) } const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString() + ' ' + new Date(dateString).toLocaleTimeString() } if (loading) { return
Loading API keys...
} return (
{error && ( {error} )} {createdKey && (

API Key Created Successfully!

Please copy this key now. You won't be able to see it again.

{createdKey.key}
)}
API Keys Manage API keys for programmatic access to the Stable Diffusion API
{showCreateForm && (
setNewKeyName(e.target.value)} placeholder="e.g., My Application Key" required />
{['generate', 'models', 'queue', 'system'].map((scope) => ( ))}
)} {apiKeys.length === 0 ? (
No API keys found. Create your first key to get started.
) : (
{apiKeys.map((apiKey) => (

{apiKey.name}

{apiKey.active ? 'Active' : 'Inactive'}

Key: {apiKey.key.substring(0, 8)}...

Created: {formatDate(apiKey.created_at)}

{apiKey.last_used && (

Last used: {formatDate(apiKey.last_used)}

)} {apiKey.expires_at && (

Expires: {formatDate(apiKey.expires_at)}

)}
{apiKey.scopes.length > 0 && (
{apiKey.scopes.map((scope) => ( {scope} ))}
)}
))}
)}
) }