// ============================================================ // email.js — Send emails via Gmail using Nodemailer // ============================================================ const nodemailer = require('nodemailer'); const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, }); // Send the OTP email to the user async function sendOTPEmail(toEmail, username, otp) { const mailOptions = { from: `"Auth System" <${process.env.EMAIL_USER}>`, to: toEmail, subject: 'Your OTP Code', html: `

One-Time Password

Hello ${username},

Your login OTP code is:

${otp}

This code expires in 5 minutes.
If you did not request this, please ignore this email.

`, }; await transporter.sendMail(mailOptions); } // Send the Change Password Link email async function sendChangePasswordLink(toEmail, username, link) { const mailOptions = { from: `"Auth System" <${process.env.EMAIL_USER}>`, to: toEmail, subject: 'Password Change Request', html: `

Change Your Password

Hello ${username},

You requested to change your password. Click the secure link below to set a new password:

Set New Password

This link expires in 15 minutes.
If you did not request this, please ignore this email and your password will remain unchanged.

`, }; await transporter.sendMail(mailOptions); } // Send a new device login alert email async function sendNewDeviceAlert(toEmail, username, deviceInfo, ipAddress, loginTime) { const timeStr = new Date(loginTime).toLocaleString('en-US', { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZoneName: 'short', }); const mailOptions = { from: `"Auth System" <${process.env.EMAIL_USER}>`, to: toEmail, subject: 'New Device Login Detected', html: `

New Device Login

Hello ${username},

We detected a new login to your account from a device we haven't seen before:

Device ${deviceInfo}
IP Address ${ipAddress}
Time ${timeStr}

If this was you, no action is needed.
If you don't recognize this login, please change your password immediately and log out of all devices from your dashboard.

`, }; await transporter.sendMail(mailOptions); } module.exports = { sendOTPEmail, sendChangePasswordLink, sendNewDeviceAlert };