'use client'; import { AdminPageHeader } from '@/components/layout/AdminPageHeader'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { SettingsField, SettingsGroup } from '@/components/ui/SettingsLayout'; import { StatStrip } from '@/components/ui/StatStrip'; import { useToast } from '@/components/ui/Toast'; import { getCached, setCached } from '@/lib/vconCache'; import Link from 'next/link'; import { useEffect, useState } from 'react'; interface AdminProfileData { email: string; role: string; storageMode: string; createdAt: string; sessionExpiresAt: string; } const DEFAULT_PROFILE: AdminProfileData = { email: 'admin@datastack.dev', role: 'Owner', storageMode: 'In-Memory', createdAt: '', sessionExpiresAt: '', }; export function ProfileWorkspace() { const { success, error } = useToast(); const [profile, setProfile] = useState(() => getCached('vcon:profile', DEFAULT_PROFILE)); const [email, setEmail] = useState(() => getCached('vcon:profile', DEFAULT_PROFILE).email); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(() => !getCached('vcon:profile-loaded', false)); const [saving, setSaving] = useState(false); const [signingOut, setSigningOut] = useState(false); useEffect(() => { fetch('/api/auth/profile', { credentials: 'include' }) .then(async (res) => { if (!res.ok) throw new Error('Unable to load profile'); return res.json(); }) .then((data) => { if (data.profile) { setProfile(setCached('vcon:profile', data.profile)); setEmail(data.profile.email); setCached('vcon:profile-loaded', true); } }) .catch(() => { error('Profile load failed', 'Could not load administrator profile.'); }) .finally(() => setLoading(false)); }, [error]); const handleSave = async (event: React.FormEvent) => { event.preventDefault(); const trimmedEmail = email.trim().toLowerCase(); if (!trimmedEmail) { error('Validation error', 'Admin email is required.'); return; } if (!currentPassword) { error('Validation error', 'Enter your current password to save changes.'); return; } if (newPassword && newPassword !== confirmPassword) { error('Validation error', 'New password and confirmation do not match.'); return; } setSaving(true); try { const response = await fetch('/api/auth/profile', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ email: trimmedEmail, currentPassword, newPassword: newPassword || undefined, }), }); const data = await response.json(); if (!response.ok) throw new Error(data.error || 'Profile update failed'); setProfile(setCached('vcon:profile', data.profile)); setEmail(data.profile.email); setCurrentPassword(''); setNewPassword(''); setConfirmPassword(''); success('Profile updated', 'Administrator credentials updated.'); } catch (saveError) { error('Update failed', saveError instanceof Error ? saveError.message : 'Unable to update profile'); } finally { setSaving(false); } }; const handleSignOut = async () => { setSigningOut(true); try { const response = await fetch('/api/auth/logout', { method: 'POST', credentials: 'include', }); if (!response.ok) throw new Error('Unable to sign out'); window.location.assign('/vcon/login'); } catch { error('Sign out failed', 'Please try again.'); setSigningOut(false); } }; return (
} />
setEmail(event.target.value)} required /> setCurrentPassword(event.target.value)} required /> setNewPassword(event.target.value)} /> setConfirmPassword(event.target.value)} />
); }