feat: email OTP para vincular correo al perfil

- EmailOtpService: genera/verifica codigo 6 digitos via SMTP
  (requiere env vars: SMTP_HOST, SMTP_USER, SMTP_PASS, EMAIL_FROM)
- POST /auth/send-email-otp: envia codigo al correo indicado
- POST /auth/link-email-otp: verifica codigo y vincula email+pass
- nodemailer agregado como dependencia

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-27 09:35:01 -05:00
co-authored by Claude Sonnet 4.6
parent d3ca99fa1b
commit 7e68fcc2bb
4 changed files with 99 additions and 3 deletions
+70
View File
@@ -0,0 +1,70 @@
import { Injectable, Logger, BadRequestException } from '@nestjs/common';
import * as nodemailer from 'nodemailer';
const OTP_TTL_MS = 10 * 60 * 1000; // 10 minutos
@Injectable()
export class EmailOtpService {
private readonly logger = new Logger(EmailOtpService.name);
private readonly otps = new Map<string, { code: string; expiresAt: number }>();
private createTransport() {
const host = process.env.SMTP_HOST;
const port = parseInt(process.env.SMTP_PORT || '587');
const user = process.env.SMTP_USER;
const pass = process.env.SMTP_PASS;
if (!host || !user || !pass) {
throw new BadRequestException(
'Correo no configurado. Añade SMTP_HOST, SMTP_USER y SMTP_PASS en las variables de entorno.',
);
}
return nodemailer.createTransport({
host,
port,
secure: port === 465,
auth: { user, pass },
});
}
async sendOtp(email: string): Promise<void> {
const code = Math.floor(100000 + Math.random() * 900000).toString();
this.otps.set(email.toLowerCase(), { code, expiresAt: Date.now() + OTP_TTL_MS });
const from = process.env.EMAIL_FROM || process.env.SMTP_USER;
const transporter = this.createTransport();
await transporter.sendMail({
from: `ProsApp <${from}>`,
to: email,
subject: 'Código de verificación ProsApp',
html: `
<div style="font-family:sans-serif;max-width:480px;margin:auto;padding:32px">
<img src="https://prosapp.co/img/logo_prosapp.png" height="40" style="margin-bottom:24px">
<h2 style="color:#1e293b;margin:0 0 8px">Verifica tu correo</h2>
<p style="color:#64748b;margin:0 0 24px">Usa este código para vincular tu correo a ProsApp. Válido por 10 minutos.</p>
<div style="background:#f0f8ff;border:1px solid #bae3ff;border-radius:12px;padding:24px;text-align:center">
<span style="font-size:36px;font-weight:700;letter-spacing:10px;color:#42A4EF">${code}</span>
</div>
<p style="color:#94a3b8;font-size:12px;margin-top:24px">Si no solicitaste esto, ignora este correo.</p>
</div>
`,
});
this.logger.log(`Email OTP enviado a ${email}`);
}
verifyOtp(email: string, code: string): boolean {
const key = email.toLowerCase();
const entry = this.otps.get(key);
if (!entry) return false;
if (Date.now() > entry.expiresAt) {
this.otps.delete(key);
return false;
}
if (entry.code !== code) return false;
this.otps.delete(key);
return true;
}
}