Files
taller_automotriz/src/lib/magic-link.ts
T
Beldor 011cdb0323 feat: migrar emails de Resend a Nodemailer + Gmail SMTP
Reemplaza Resend por nodemailer con Gmail SMTP. El to: ahora usa
el email real del cliente en lugar del hardcodeado de sandbox.
Variables de entorno: GMAIL_USER y GMAIL_APP_PASSWORD.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-18 18:03:13 -03:00

67 lines
1.9 KiB
TypeScript

import { prisma } from "./prisma";
import nodemailer from "nodemailer";
import crypto from "crypto";
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_APP_PASSWORD,
},
});
export async function enviarMagicLink(email: string): Promise<{ ok: boolean; error?: string }> {
const cliente = await prisma.cliente.findUnique({ where: { email } });
if (!cliente) return { ok: false, error: "Email no registrado" };
await prisma.magicToken.updateMany({
where: { clienteId: cliente.id, usado: false },
data: { usado: true },
});
const token = crypto.randomBytes(32).toString("hex");
const expiresAt = new Date(Date.now() + 15 * 60 * 1000);
await prisma.magicToken.create({
data: { clienteId: cliente.id, token, expiresAt },
});
const link = `${process.env.MAGIC_LINK_BASE_URL}/cliente/auth?token=${token}`;
await transporter.sendMail({
from: `"Taller" <${process.env.GMAIL_USER}>`,
to: email,
subject: "Tu link de acceso al taller",
html: `
<h2>Hola ${cliente.nombre}!</h2>
<p>Hacé click en el siguiente botón para acceder a tu cuenta:</p>
<a href="${link}" style="background:#2563eb;color:white;padding:12px 24px;border-radius:6px;text-decoration:none;display:inline-block">
Acceder al taller
</a>
<p style="color:#6b7280;font-size:14px">Este link expira en 15 minutos. Si no lo pediste, ignorá este email.</p>
`,
});
return { ok: true };
}
export async function validarMagicToken(token: string) {
const registro = await prisma.magicToken.findUnique({
where: { token },
include: { cliente: true },
});
if (!registro || registro.usado || registro.expiresAt < new Date()) {
return null;
}
await prisma.magicToken.update({
where: { id: registro.id },
data: { usado: true },
});
return registro.cliente;
}