diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 904715a..6fd7d59 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -7,6 +7,7 @@ import { JwtService } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; import { PrismaService } from '../prisma/prisma.service'; import { nanoid } from 'nanoid'; +import * as nodemailer from 'nodemailer'; @Injectable() export class AuthService { @@ -29,8 +30,46 @@ export class AuthService { }, }); - // TODO: integrate real email service (SMTP, SendGrid, etc.) - console.log(`Magic link code for ${email}: ${code}`); + const smtpHost = this.config.get('SMTP_HOST'); + const smtpPort = this.config.get('SMTP_PORT', 587); + const smtpUser = this.config.get('SMTP_USER'); + const smtpPass = this.config.get('SMTP_PASS'); + const mailFrom = this.config.get( + 'MAIL_FROM', + 'radiOLA ', + ); + + if (smtpHost && smtpUser && smtpPass) { + try { + const transporter = nodemailer.createTransport({ + host: smtpHost, + port: smtpPort, + secure: smtpPort === 465, + auth: { + user: smtpUser, + pass: smtpPass, + }, + authMethod: 'LOGIN', + tls: { + rejectUnauthorized: false, + }, + }); + + await transporter.sendMail({ + from: mailFrom, + to: email, + subject: 'Ваш код подтверждения radiOLA', + text: `Ваш код подтверждения: ${code}\n\nКод действителен 15 минут.`, + html: `

Ваш код подтверждения: ${code}

Код действителен 15 минут.

`, + }); + } catch (error) { + console.error('Failed to send email:', error.message); + // Fallback to console log for debugging + console.log(`Magic link code for ${email}: ${code}`); + } + } else { + console.log(`Magic link code for ${email}: ${code}`); + } return { message: 'Check your email for the verification code.' }; }