File size: 2,423 Bytes
2b62b0c
bb6e880
 
2b62b0c
 
 
 
 
 
 
 
 
bb6e880
2b62b0c
 
 
 
 
 
 
 
 
 
 
bb6e880
 
2b62b0c
 
bb6e880
 
 
 
 
 
 
 
 
 
 
 
 
 
0118bca
 
 
 
2b62b0c
 
 
 
 
 
bb6e880
 
0118bca
bb6e880
 
 
 
 
2b62b0c
bb6e880
 
 
 
 
 
 
0118bca
 
bb6e880
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { env } from "@pram/env/server";
import nodemailer from "nodemailer";

const smtpConfigured =
  env.SMTP_HOST && env.SMTP_USER && env.SMTP_PASS && env.SMTP_FROM;

if (!smtpConfigured) {
  console.warn(
    "[email] SMTP not configured — email features (verification, password reset) will be disabled. Set SMTP_HOST, SMTP_USER, SMTP_PASS, SMTP_FROM in apps/server/.env to enable."
  );
}

const smtpPort = Number(env.SMTP_PORT) || 587;
const transporter = smtpConfigured
  ? nodemailer.createTransport({
      host: env.SMTP_HOST,
      port: smtpPort,
      secure: smtpPort === 465,
      auth: {
        user: env.SMTP_USER,
        pass: env.SMTP_PASS,
      },
    })
  : null;

const subjects: Record<string, string> = {
  "email-verification": "Verifikasi Email Pram",
  "forget-password": "Reset Password Pram",
};

const messages: Record<string, string> = {
  "email-verification": "Gunakan kode berikut untuk memverifikasi email Anda:",
  "forget-password": "Gunakan kode berikut untuk mereset password Anda:",
};

type SendOtpEmailProps = {
  to: string;
  otp: string;
  type: "email-verification" | "forget-password";
};

export async function sendOtpEmail({ to, otp, type }: SendOtpEmailProps) {
  console.log(`\n======================================================`);
  console.log(`[BYPASS EMAIL] Kode OTP untuk ${to} adalah: ${otp}`);
  console.log(`======================================================\n`);

  if (!transporter) {
    console.warn(`[email] Skipping OTP email to ${to} — SMTP not configured.`);
    return;
  }

  const subject = subjects[type] || "Kode Verifikasi Pram";
  const message = messages[type] || "Kode verifikasi Anda:";

  transporter.sendMail({
    from: env.SMTP_FROM,
    to,
    subject,
    html: `
      <div style="font-family: sans-serif; max-width: 400px; margin: 0 auto; padding: 20px;">
        <h2 style="color: #16a34a;">Pram</h2>
        <p>${message}</p>
        <div style="background: #f3f4f6; padding: 20px; border-radius: 8px; text-align: center; margin: 20px 0;">
          <span style="font-size: 32px; font-weight: bold; letter-spacing: 8px; color: #111827;">${otp}</span>
        </div>
        <p style="color: #6b7280; font-size: 14px;">Kode berlaku selama 5 menit. Jangan bagikan kode ini kepada siapapun.</p>
      </div>
    `,
  }).catch((err) => {
    console.error("[email] Error sending email (background):", err.message);
  });
}