'use client'; import { useState, useEffect } from 'react'; import { useLanguage } from '@/context/LanguageContext'; import { useAuth } from '@/context/AuthContext'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Input from '@/components/ui/Input'; import { dashboardApi, authApi, UserProfile, UserSession } from '@/lib/api'; import toast from 'react-hot-toast'; export default function SecurityTab() { const { locale: language } = useLanguage(); const { logout } = useAuth(); const [profile, setProfile] = useState(null); const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); const [changingPassword, setChangingPassword] = useState(false); const [settingPassword, setSettingPassword] = useState(false); const [passwordForm, setPasswordForm] = useState({ currentPassword: '', newPassword: '', confirmPassword: '', }); const [newPasswordForm, setNewPasswordForm] = useState({ password: '', confirmPassword: '', }); useEffect(() => { loadData(); }, []); const loadData = async () => { try { const [profileRes, sessionsRes] = await Promise.all([ dashboardApi.getProfile(), dashboardApi.getSessions(), ]); setProfile(profileRes.profile); setSessions(sessionsRes.sessions); } catch (error) { toast.error(language === 'es' ? 'Error al cargar datos' : 'Failed to load data'); } finally { setLoading(false); } }; const handleChangePassword = async (e: React.FormEvent) => { e.preventDefault(); if (passwordForm.newPassword !== passwordForm.confirmPassword) { toast.error(language === 'es' ? 'Las contraseñas no coinciden' : 'Passwords do not match'); return; } if (passwordForm.newPassword.length < 10) { toast.error(language === 'es' ? 'La contraseña debe tener al menos 10 caracteres' : 'Password must be at least 10 characters'); return; } setChangingPassword(true); try { await authApi.changePassword(passwordForm.currentPassword, passwordForm.newPassword); toast.success(language === 'es' ? 'Contraseña actualizada' : 'Password updated'); setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' }); } catch (error: any) { toast.error(error.message || (language === 'es' ? 'Error al cambiar contraseña' : 'Failed to change password')); } finally { setChangingPassword(false); } }; const handleSetPassword = async (e: React.FormEvent) => { e.preventDefault(); if (newPasswordForm.password !== newPasswordForm.confirmPassword) { toast.error(language === 'es' ? 'Las contraseñas no coinciden' : 'Passwords do not match'); return; } if (newPasswordForm.password.length < 10) { toast.error(language === 'es' ? 'La contraseña debe tener al menos 10 caracteres' : 'Password must be at least 10 characters'); return; } setSettingPassword(true); try { await dashboardApi.setPassword(newPasswordForm.password); toast.success(language === 'es' ? 'Contraseña establecida' : 'Password set'); setNewPasswordForm({ password: '', confirmPassword: '' }); loadData(); // Reload to update profile } catch (error: any) { toast.error(error.message || (language === 'es' ? 'Error al establecer contraseña' : 'Failed to set password')); } finally { setSettingPassword(false); } }; const handleUnlinkGoogle = async () => { if (!confirm(language === 'es' ? '¿Estás seguro de que quieres desvincular tu cuenta de Google?' : 'Are you sure you want to unlink your Google account?' )) { return; } try { await dashboardApi.unlinkGoogle(); toast.success(language === 'es' ? 'Google desvinculado' : 'Google unlinked'); loadData(); } catch (error: any) { toast.error(error.message || (language === 'es' ? 'Error' : 'Failed')); } }; const handleRevokeSession = async (sessionId: string) => { try { await dashboardApi.revokeSession(sessionId); setSessions(sessions.filter((s) => s.id !== sessionId)); toast.success(language === 'es' ? 'Sesión cerrada' : 'Session revoked'); } catch (error) { toast.error(language === 'es' ? 'Error' : 'Failed'); } }; const handleRevokeAllSessions = async () => { if (!confirm(language === 'es' ? '¿Cerrar todas las sesiones? Serás desconectado.' : 'Log out of all sessions? You will be logged out.' )) { return; } try { await dashboardApi.revokeAllSessions(); toast.success(language === 'es' ? 'Todas las sesiones cerradas' : 'All sessions revoked'); logout(); } catch (error) { toast.error(language === 'es' ? 'Error' : 'Failed'); } }; const formatDate = (dateStr: string) => { return new Date(dateStr).toLocaleString(language === 'es' ? 'es-ES' : 'en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZone: 'America/Asuncion', }); }; if (loading) { return (
); } return (
{/* Authentication Methods */}

{language === 'es' ? 'Métodos de Autenticación' : 'Authentication Methods'}

{/* Password Status */}

{language === 'es' ? 'Contraseña' : 'Password'}

{profile?.hasPassword ? (language === 'es' ? 'Configurada' : 'Set') : (language === 'es' ? 'No configurada' : 'Not set')}

{profile?.hasPassword && ( {language === 'es' ? 'Activo' : 'Active'} )}
{/* Google Status */}

Google

{profile?.hasGoogleLinked ? (language === 'es' ? 'Vinculado' : 'Linked') : (language === 'es' ? 'No vinculado' : 'Not linked')}

{profile?.hasGoogleLinked && profile?.hasPassword && ( )}
{/* Password Management */}

{profile?.hasPassword ? (language === 'es' ? 'Cambiar Contraseña' : 'Change Password') : (language === 'es' ? 'Establecer Contraseña' : 'Set Password')}

{profile?.hasPassword ? (
setPasswordForm({ ...passwordForm, currentPassword: e.target.value })} required /> setPasswordForm({ ...passwordForm, newPassword: e.target.value })} required />

{language === 'es' ? 'Mínimo 10 caracteres' : 'Minimum 10 characters'}

setPasswordForm({ ...passwordForm, confirmPassword: e.target.value })} required />
) : (

{language === 'es' ? 'Actualmente inicias sesión con Google. Establece una contraseña para más opciones de acceso.' : 'You currently sign in with Google. Set a password for more access options.'}

setNewPasswordForm({ ...newPasswordForm, password: e.target.value })} required />

{language === 'es' ? 'Mínimo 10 caracteres' : 'Minimum 10 characters'}

setNewPasswordForm({ ...newPasswordForm, confirmPassword: e.target.value })} required />
)}
{/* Active Sessions */}

{language === 'es' ? 'Sesiones Activas' : 'Active Sessions'}

{sessions.length > 1 && ( )}
{sessions.length === 0 ? (

{language === 'es' ? 'No hay sesiones activas' : 'No active sessions'}

) : (
{sessions.map((session, index) => (

{session.userAgent ? session.userAgent.substring(0, 50) + (session.userAgent.length > 50 ? '...' : '') : (language === 'es' ? 'Dispositivo desconocido' : 'Unknown device')}

{language === 'es' ? 'Última actividad:' : 'Last active:'} {formatDate(session.lastActiveAt)} {session.ipAddress && ` • ${session.ipAddress}`}

{index !== 0 && ( )} {index === 0 && ( {language === 'es' ? 'Esta sesión' : 'This session'} )}
))}
)}
{/* Danger Zone */}

{language === 'es' ? 'Zona de Peligro' : 'Danger Zone'}

{language === 'es' ? 'Si deseas eliminar tu cuenta, contacta al soporte.' : 'If you want to delete your account, please contact support.'}

); }