'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { createProjectApiKey, listApiKeys } from '@/lib/actions/apiKeyActions'; import KeyDisplay from '@/components/admin/KeyDisplay'; import AdminFormInput from '@/components/admin/AdminFormInput'; import AdminButton from '@/components/admin/AdminButton'; import { Section } from '@/components/shared/Section'; const STORAGE_KEY = 'banatie_master_key'; export default function ApiKeysPage() { const router = useRouter(); const [masterKey, setMasterKey] = useState(''); const [orgSlug, setOrgSlug] = useState(''); const [projectSlug, setProjectSlug] = useState(''); const [generatedKey, setGeneratedKey] = useState(''); const [apiKeys, setApiKeys] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); // Check for master key on mount useEffect(() => { const saved = localStorage.getItem(STORAGE_KEY); if (!saved) { router.push('/admin/master'); } else { setMasterKey(saved); // Load API keys with the saved master key listApiKeys(saved).then(setApiKeys); } }, [router]); const loadApiKeys = async () => { if (masterKey) { const keys = await listApiKeys(masterKey); setApiKeys(keys); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); setSuccess(''); setGeneratedKey(''); const result = await createProjectApiKey(masterKey, orgSlug, projectSlug); if (result.success && result.apiKey) { setGeneratedKey(result.apiKey); setSuccess('API key created successfully!'); // Clear form setOrgSlug(''); setProjectSlug(''); // Reload keys list await loadApiKeys(); } else { setError(result.error || 'Failed to create API key'); } setLoading(false); }; if (!masterKey) { return null; // Will redirect } return (
{/* Page Header */}

Project API Keys

Generate API keys for your projects. Organizations and projects will be created automatically if they don't exist.

{/* Messages */} {error && (
{error}
)} {success && (
{success}
)} {/* Generated Key Display */} {generatedKey && (

New API Key Generated

⚠️ Save this key now! You won't be able to see it again.

)} {/* Create Key Form */}

Create New API Key

{loading ? 'Creating...' : 'Create API Key'}
{/* API Keys List */}

Recent API Keys

{apiKeys.length === 0 ? (

No API keys created yet.

) : (
{apiKeys.map((key) => ( ))}
Type Organization Project Created Expires Status
{key.keyType} {key.organizationName || '-'} {key.organizationEmail && (
{key.organizationEmail}
)}
{key.projectName || '-'} {new Date(key.createdAt).toLocaleDateString()} {key.expiresAt ? new Date(key.expiresAt).toLocaleDateString() : 'Never'} {key.isActive ? 'Active' : 'Inactive'}
)}
); }