almeira-web/zapply-app/src/app/api/auth/toggle-2fa/route.ts

42 lines
1.1 KiB
TypeScript

import { NextResponse } from 'next/server';
import { dataService } from '@/lib/data-service';
export async function POST(request: Request) {
try {
const { userId, twoFactorEnabled, twoFactorMethod } = await request.json();
if (!userId) {
return NextResponse.json(
{ success: false, error: 'ID de usuario requerido' },
{ status: 400 }
);
}
const updated = dataService.updateUser(userId, {
twoFactorEnabled: !!twoFactorEnabled,
twoFactorMethod: twoFactorMethod || 'whatsapp_otp',
});
if (!updated) {
return NextResponse.json(
{ success: false, error: 'Usuario no encontrado' },
{ status: 404 }
);
}
return NextResponse.json({
success: true,
message: twoFactorEnabled
? '🛡️ Autenticación en dos factores (2FA) activada con éxito'
: '⚪ Autenticación en dos factores (2FA) desactivada',
user: updated,
});
} catch (error: any) {
console.error('Error toggling 2FA:', error);
return NextResponse.json(
{ success: false, error: error.message || 'Error en el servidor' },
{ status: 500 }
);
}
}