"use client"; import { useEffect, useState, useCallback } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; interface User { id: string; name: string; email: string; emailVerified: boolean; image: string | null; createdAt: string; updatedAt: string; } interface ApiKeyRow { id: string; name: string; keyPreview: string; expiresAt: string | null; daysRemaining: number | null; remainingLabel: string; lastUsedAt: string | null; isActive: boolean; createdAt: string; } export default function ProfilePage() { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const [apiKeys, setApiKeys] = useState([]); const [keysLoading, setKeysLoading] = useState(false); const [newKeyName, setNewKeyName] = useState(""); const [newKeyDays, setNewKeyDays] = useState(""); const [createBusy, setCreateBusy] = useState(false); const router = useRouter(); const loadApiKeys = useCallback(async () => { setKeysLoading(true); try { const res = await fetch("/api/v1/api-keys", { credentials: "include" }); const json = await res.json(); if (res.ok && json.data?.keys) { setApiKeys(json.data.keys); } } catch { /* ignore */ } finally { setKeysLoading(false); } }, []); useEffect(() => { const fetchUser = async () => { try { const response = await fetch("/api/auth/get-session", { credentials: "include", }); const data = await response.json(); if (!response.ok || !data.user) { router.push("/login"); return; } setUser(data.user); } catch (error) { console.error("Kullanıcı bilgileri alınamadı:", error); router.push("/login"); } finally { setLoading(false); } }; fetchUser(); }, [router]); useEffect(() => { if (user) { loadApiKeys(); } }, [user, loadApiKeys]); const createApiKey = async () => { const name = newKeyName.trim(); if (!name) return; setCreateBusy(true); try { const body: { name: string; expiresInDays?: number | null } = { name }; const d = newKeyDays.trim(); if (d !== "") { const n = parseInt(d, 10); if (!Number.isFinite(n) || n < 0) { alert("Geçerli bir gün sayısı girin veya boş bırakın (süresiz)."); setCreateBusy(false); return; } body.expiresInDays = n === 0 ? null : n; } const res = await fetch("/api/v1/api-keys", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); const json = await res.json(); if (!res.ok) { alert(json.error || "Oluşturulamadı"); return; } const raw = json.data?.key as string | undefined; const rem = json.data?.remainingLabel as string | undefined; if (raw) { await navigator.clipboard.writeText(raw).catch(() => {}); const extra = rem ? ` Süre: ${rem}.` : ""; alert( `API anahtarı oluşturuldu ve panoya kopyalandı. Bu tam değeri yalnızca bir kez görebilirsiniz.${extra}` ); } setNewKeyName(""); setNewKeyDays(""); await loadApiKeys(); } finally { setCreateBusy(false); } }; const revokeKey = async (id: string) => { if (!confirm("Bu API anahtarını iptal etmek istediğinize emin misiniz?")) return; const res = await fetch(`/api/v1/api-keys/${id}`, { method: "DELETE", credentials: "include", }); if (!res.ok) { const j = await res.json(); alert(j.error || "İptal edilemedi"); return; } await loadApiKeys(); }; const handleLogout = async () => { try { await fetch("/api/auth/sign-out", { method: "POST", headers: { "Content-Type": "application/json", }, credentials: "include", body: JSON.stringify({}), }); router.push("/login"); router.refresh(); } catch (error) { console.error("Çıkış yapılamadı:", error); } }; if (loading) { return (
Yükleniyor...
); } if (!user) { return null; } return (

Kullanıcı Bilgileri

Resim Yükle Ana Sayfa

Profil Bilgileri

Ad Soyad
{user.name}
E-posta
{user.email}
E-posta Doğrulandı mı?
{user.emailVerified ? ( Evet ) : ( Hayır )}
Kullanıcı ID
{user.id}
Kayıt Tarihi
{new Date(user.createdAt).toLocaleString("tr-TR")}
Son Güncelleme
{new Date(user.updatedAt).toLocaleString("tr-TR")}

API anahtarları

Resim API'sine JWT yerine{" "} Authorization: Bearer img_… {" "} ile erişin. Anahtarı burada oluşturun; süre kısıtı opsiyoneldir (boş = süresiz). Admin gerekirse süreyi değiştirebilir.

setNewKeyName(e.target.value)} placeholder="örn. Mobil uygulama" className="w-full rounded border border-gray-300 bg-white px-3 py-2 text-black dark:border-zinc-600 dark:bg-zinc-900 dark:text-zinc-50" />
setNewKeyDays(e.target.value)} className="w-full rounded border border-gray-300 bg-white px-3 py-2 text-black dark:border-zinc-600 dark:bg-zinc-900 dark:text-zinc-50" />
{keysLoading ? (

Anahtarlar yükleniyor…

) : apiKeys.length === 0 ? (

Henüz API anahtarı yok.

) : (
{apiKeys.map((k) => ( ))}
İsim Önizleme Bitiş tarihi Kalan süre Durum
{k.name} {k.keyPreview} {k.expiresAt ? new Date(k.expiresAt).toLocaleString("tr-TR") : "—"} {k.remainingLabel} {k.isActive ? ( Aktif ) : ( İptal )} {k.isActive && ( )}
)}
); }