"use client";
import { useState, useEffect } from "react";
import Link from "next/link";
import Button from "@/components/shared/Button";
export default function APIKeysPage() {
const [apiKeys, setApiKeys] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetchAPIKeys();
}, []);
async function fetchAPIKeys() {
try {
const response = await fetch(
"http://localhost:8080/twirp/apikeys.APIKeysService/ListAPIKeys",
{
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
},
);
if (!response.ok) throw new Error("Failed to fetch API keys");
const data = await response.json();
setApiKeys(data);
} catch (error) {
setError("Failed to load API keys");
console.error("Error:", error);
} finally {
setIsLoading(false);
}
}
if (isLoading) {
return (
Loading API keys...
);
}
if (error) {
return (
{error}
);
}
async function handleRevokeKey(keyId) {
if (
!confirm(
"Are you sure you want to revoke this API key? This action cannot be undone.",
)
) {
return;
}
try {
const response = await fetch(
`http://localhost:8080/twirp/apikeys.APIKeysService/RevokeAPIKey`,
{
method: "POST",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ keyId }),
},
);
if (!response.ok) throw new Error("Failed to revoke API key");
await fetchAPIKeys(); // Refresh the list
} catch (error) {
setError("Failed to revoke API key");
console.error("Error:", error);
}
}
if (!apiKeys.length) {
return (
No API keys
Create an API key to get started with programmatic access.