/
fegerV
/
Stogram
Обзор
Документация
Войти
/
fegerV
/
Stogram
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
server/src/services/emailService.ts
101 строка
4 KB
engine-labs-app[bot]
feat(messaging): add reactions, scheduled messages, email verification, media enhancements, and push notifications
31 окт 2025, 18:54
31 окт 2025, 18:54
8509248
Код
Авторство
О чём код?
import nodemailer from 'nodemailer'; import crypto from 'crypto'; const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST || 'smtp.gmail.com', port: parseInt(process.env.SMTP_PORT || '587'), secure: process.env.SMTP_SECURE === 'true', auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS, }, }); export const generateVerificationToken = (): string => { return crypto.randomBytes(32).toString('hex'); }; export const sendVerificationEmail = async ( email: string, token: string, username: string ): Promise<void> => { const verificationUrl = `${process.env.CLIENT_URL || 'http://localhost:5173'}/verify-email?token=${token}`; const mailOptions = { from: process.env.SMTP_FROM || process.env.SMTP_USER, to: email, subject: 'Verify your Stogram email', html: ` <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"> <h2 style="color: #0088cc;">Welcome to Stogram, ${username}!</h2> <p>Thank you for registering. Please verify your email address by clicking the button below:</p> <div style="text-align: center; margin: 30px 0;"> <a href="${verificationUrl}" style="background-color: #0088cc; color: white; padding: 12px 30px; text-decoration: none; border-radius: 5px; display: inline-block;"> Verify Email </a> </div> <p>Or copy and paste this link into your browser:</p> <p style="word-break: break-all; color: #666;">${verificationUrl}</p> <p>This link will expire in 24 hours.</p> <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;"> <p style="color: #999; font-size: 12px;"> If you didn't create this account, please ignore this email. </p> </div> `, }; try { await transporter.sendMail(mailOptions); console.log(`Verification email sent to ${email}`); } catch (error) { console.error('Error sending verification email:', error); throw error; } }; export const sendPasswordResetEmail = async ( email: string, token: string, username: string ): Promise<void> => { const resetUrl = `${process.env.CLIENT_URL || 'http://localhost:5173'}/reset-password?token=${token}`; const mailOptions = { from: process.env.SMTP_FROM || process.env.SMTP_USER, to: email, subject: 'Reset your Stogram password', html: ` <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"> <h2 style="color: #0088cc;">Password Reset Request</h2> <p>Hi ${username},</p> <p>We received a request to reset your password. Click the button below to create a new password:</p> <div style="text-align: center; margin: 30px 0;"> <a href="${resetUrl}" style="background-color: #0088cc; color: white; padding: 12px 30px; text-decoration: none; border-radius: 5px; display: inline-block;"> Reset Password </a> </div> <p>Or copy and paste this link into your browser:</p> <p style="word-break: break-all; color: #666;">${resetUrl}</p> <p>This link will expire in 1 hour.</p> <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;"> <p style="color: #999; font-size: 12px;"> If you didn't request a password reset, please ignore this email and your password will remain unchanged. </p> </div> `, }; try { await transporter.sendMail(mailOptions); console.log(`Password reset email sent to ${email}`); } catch (error) { console.error('Error sending password reset email:', error); throw error; } };