'use client'; import { useState, useEffect, useRef } from 'react'; import Header from '@/components/Header'; import { motion, AnimatePresence } from 'framer-motion'; import { Fingerprint, Lock, User, Save, Camera } from 'lucide-react'; import { startRegistration } from '@simplewebauthn/browser'; export default function SettingsPage() { const [user, setUser] = useState(null); const [name, setName] = useState(''); const [password, setPassword] = useState(''); const [avatarBase64, setAvatarBase64] = useState(null); const [biometricEnabled, setBiometricEnabled] = useState(false); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [isError, setIsError] = useState(false); const fileInputRef = useRef(null); useEffect(() => { const userData = localStorage.getItem('user'); if (userData) { const parsedUser = JSON.parse(userData); setUser(parsedUser); setName(parsedUser.name || ''); } const bio = localStorage.getItem('biometric_enabled'); if (bio === 'true') setBiometricEnabled(true); }, []); const showToast = (msg, error = false) => { setMessage(msg); setIsError(error); setTimeout(() => setMessage(''), 4000); }; const handleBiometricToggle = async () => { if (biometricEnabled) { setBiometricEnabled(false); localStorage.setItem('biometric_enabled', 'false'); showToast('ورود با اثر انگشت غیرفعال شد'); return; } try { showToast('در حال آماده‌سازی سنسور...', false); const res = await fetch('/api/auth/webauthn/register/generate-options', { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); const options = await res.json(); if (options.error) throw new Error(options.error); // Trigger native biometric prompt const attResp = await startRegistration(options); const verificationRes = await fetch('/api/auth/webauthn/register/verify', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(attResp), }); const verification = await verificationRes.json(); if (verification.verified) { setBiometricEnabled(true); localStorage.setItem('biometric_enabled', 'true'); showToast('اثر انگشت با موفقیت فعال شد'); } else { throw new Error('Verification failed'); } } catch (error) { console.error(error); showToast('خطا در فعال‌سازی یا لغو توسط کاربر', true); } }; const handleFileChange = (e) => { const file = e.target.files[0]; if (file) { const reader = new FileReader(); reader.onloadend = () => setAvatarBase64(reader.result); reader.readAsDataURL(file); } }; const handleSave = async () => { setLoading(true); try { const res = await fetch('/api/user/settings', { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify({ name, password: password ? password : undefined, avatarUrl: avatarBase64 ? avatarBase64 : undefined }) }); const data = await res.json(); if (res.ok) { localStorage.setItem('user', JSON.stringify(data.user)); setUser(data.user); setPassword(''); showToast('تغییرات شما با موفقیت ذخیره شد.'); } else { showToast(data.error || 'خطا در ذخیره تغییرات', true); } } catch (err) { showToast('خطا در ارتباط با سرور', true); } finally { setLoading(false); } }; const container = { hidden: { opacity: 0 }, show: { opacity: 1, transition: { staggerChildren: 0.1 } } }; const item = { hidden: { opacity: 0, y: 10 }, show: { opacity: 1, y: 0 } }; return (
{/* Global Toast Notification */} {message && ( {message} )}
avatar

{user?.name || 'کاربر'}

{user?.roles?.includes('ADMIN') ? 'مدیریت کل' : 'کاربر عادی'}

مشخصات کاربری

setName(e.target.value)} className="bg-transparent border-none text-sm font-bold text-gray-800 outline-none w-full" placeholder="نام شما" />
setPassword(e.target.value)} className="bg-transparent border-none text-sm font-bold text-gray-800 outline-none w-full placeholder-gray-300" placeholder="••••••••" />

امنیت

ورود با اثر انگشت Face ID / Touch ID
); }