From aaec46c091fe951047c27f19a421a3acd0375793 Mon Sep 17 00:00:00 2001 From: MarcusVinix Date: Wed, 21 Jun 2023 17:39:03 -0300 Subject: [PATCH] improve email service and add it to the user service. --- api/package.json | 1 - api/src/auth/auth.service.ts | 48 +++---------------- api/src/mailing/dto/email.dto.ts | 5 ++ api/src/mailing/mailing.controller.ts | 8 +++- api/src/mailing/mailing.service.ts | 11 +++-- api/src/user/user.controller.ts | 66 +++++++++++---------------- api/src/user/user.module.ts | 2 + package-lock.json | 1 - 8 files changed, 54 insertions(+), 88 deletions(-) create mode 100644 api/src/mailing/dto/email.dto.ts diff --git a/api/package.json b/api/package.json index de7399f6..b4456241 100644 --- a/api/package.json +++ b/api/package.json @@ -36,7 +36,6 @@ "class-validator": "^0.13.2", "googleapis": "^118.0.0", "handlebars": "^4.7.7", - "nodemailer": "^6.9.3", "passport": "^0.6.0", "passport-jwt": "^4.0.0", "passport-local": "^1.0.0", diff --git a/api/src/auth/auth.service.ts b/api/src/auth/auth.service.ts index 711a9160..36b5332f 100644 --- a/api/src/auth/auth.service.ts +++ b/api/src/auth/auth.service.ts @@ -11,13 +11,12 @@ import { UserDto } from 'src/user/dto/user.dto'; import { HttpService } from '@nestjs/axios'; import { CreateUserDto } from 'src/user/dto/create-user.dto'; import { SignInUserDto } from './dto/SignInUser.dto'; -// import { smtpConfig } from 'src/config/smtp'; -// import * as nodemailer from 'nodemailer'; import { generateCode } from 'src/utils/utils'; import * as bcrypt from 'bcrypt'; import { ChangePasswordDto } from './dto/ChangePassword.dto'; import { RecoveryPasswordDto } from './dto/RecoveryPassword.dto'; import { MailingService } from 'src/mailing/mailing.service'; +import { EmailDto } from 'src/mailing/dto/email.dto'; @Injectable() @@ -254,46 +253,13 @@ export class AuthService { `; - await this.mailingService.sendMail(user.email as string, 'Recovery password Code from Transcendence', body); - // const transporter = nodemailer.createTransport({ - // service: 'gmail', - // port: smtpConfig.port, - // secure: true, - // logger: false, - // debug: false, - // auth: { - // user: process.env['API_EMAIL_USER'], - // pass: process.env['API_EMAIL_PASS'], - // }, - // tls: { - // rejectUnauthorized: false, - // } - // }); - // await transporter.sendMail({ - // from: process.env['API_EMAIL_FROM'], - // to: [user.email as string], - // subject: 'Recovery password Code from Transcendence', - // text: `Your recovery password code is '${sendedCode}'`, - // html: ` - //
- //

- // Hello, ${user.nick} - //

- //

- // You have requested to change the password. - //

- //

- // Your change password code is: - //

- //
- // ${sendedCode} - //
- //
- // `, - // }); + const emailDto: EmailDto = { + body: body, + subject: 'Recovery password Code from Transcendence', + emailTo: [user.email as string] + }; + await this.mailingService.sendMail(emailDto); return ; } diff --git a/api/src/mailing/dto/email.dto.ts b/api/src/mailing/dto/email.dto.ts new file mode 100644 index 00000000..bfbe4a66 --- /dev/null +++ b/api/src/mailing/dto/email.dto.ts @@ -0,0 +1,5 @@ +export class EmailDto { + emailTo: string[]; + subject: string; + body: string; +} \ No newline at end of file diff --git a/api/src/mailing/mailing.controller.ts b/api/src/mailing/mailing.controller.ts index 7d6581b8..59e17966 100644 --- a/api/src/mailing/mailing.controller.ts +++ b/api/src/mailing/mailing.controller.ts @@ -1,5 +1,6 @@ import { Controller, Get } from '@nestjs/common'; import { MailingService } from './mailing.service'; +import { EmailDto } from './dto/email.dto'; @Controller('mailing') export class MailingController { @@ -8,7 +9,12 @@ export class MailingController { @Get('send-mail') public sendMail() { - this.mailingService.sendMail('buccky.live8@gmail.com', 'none', 'none'); + const emailDto: EmailDto = { + body: 'teste', + subject: 'teste', + emailTo: ['teste'] + }; + this.mailingService.sendMail(emailDto); } } diff --git a/api/src/mailing/mailing.service.ts b/api/src/mailing/mailing.service.ts index f7854dfe..bbd79251 100644 --- a/api/src/mailing/mailing.service.ts +++ b/api/src/mailing/mailing.service.ts @@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { google } from 'googleapis'; import { Options } from 'nodemailer/lib/smtp-transport'; +import { EmailDto } from './dto/email.dto'; @Injectable() export class MailingService { @@ -46,15 +47,15 @@ export class MailingService { this.mailerService.addTransporter('gmail', config); } - public async sendMail(emailTo: string, subject: string, body: string) { + public async sendMail(emailDto: EmailDto) { await this.setTransport(); this.mailerService .sendMail({ transporterName: 'gmail', - to: emailTo, // list of receivers - from: 'ft.transcendence.42sp@gmail.com', // sender address - subject: subject, // Subject line - html: body, + to: emailDto.emailTo, + from: 'ft.transcendence.42sp@gmail.com', + subject: emailDto.subject, + html: emailDto.body, }) .then((success) => { console.log(success); diff --git a/api/src/user/user.controller.ts b/api/src/user/user.controller.ts index bdf98366..06ff8ffd 100644 --- a/api/src/user/user.controller.ts +++ b/api/src/user/user.controller.ts @@ -22,8 +22,6 @@ import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; import { UserService } from './user.service'; -import * as nodemailer from 'nodemailer'; -import { smtpConfig } from '../config/smtp'; import { UserDto, UserHistoricDto } from './dto/user.dto'; import * as bcrypt from 'bcrypt'; import { FriendRequestDto } from './dto/friend-request.dto'; @@ -31,12 +29,15 @@ import { GetFriendDto } from './dto/get-friend.dto'; import { NotifyHandlerDto } from 'src/notification/dto/notify-dto'; import { generateCode, getAssetsPath } from 'src/utils/utils'; import { ChallengeRequestDto } from './dto/challenge-request.dto'; +import { EmailDto } from 'src/mailing/dto/email.dto'; +import { MailingService } from 'src/mailing/mailing.service'; @Controller('api/user') @ApiTags('user') export class UserController { constructor( private readonly userService: UserService, + readonly mailingService: MailingService ) { } @Post() @@ -101,45 +102,32 @@ export class UserController { const sendedCode = generateCode(); updateUserDto.tfaCode = sendedCode; const user = await this.userService.updateUser(updateUserDto, userFromJwt.email); - const transporter = nodemailer.createTransport({ - service: 'gmail', - port: smtpConfig.port, - secure: true, - logger: false, - debug: false, - auth: { - user: process.env['API_EMAIL_USER'], - pass: process.env['API_EMAIL_PASS'], - }, - tls: { - rejectUnauthorized: false, - } - }); + if (user.tfaEmail) { - await transporter.sendMail({ - from: process.env['API_EMAIL_FROM'], - to: [user.tfaEmail as string], + const body = ` +
+

+ Hello, ${user.nick} +

+

+ You have requested to enable Two-Factor Authentication +

+

+ Your activation code is: +

+
+ ${sendedCode} +
+
+ `; + const emailDto: EmailDto = { + body: body, subject: 'Verify Code from Transcendence', - text: `Your validation code is '${sendedCode}'`, - html: ` -
-

- Hello, ${user.nick} -

-

- You have requested to enable Two-Factor Authentication -

-

- Your activation code is: -

-
- ${sendedCode} -
-
- `, - }); + emailTo: [user.email as string] + }; + await this.mailingService.sendMail(emailDto); } else { throw new InternalServerErrorException('Error: Mail can\'t be empty'); } diff --git a/api/src/user/user.module.ts b/api/src/user/user.module.ts index 884518e1..89f6b654 100644 --- a/api/src/user/user.module.ts +++ b/api/src/user/user.module.ts @@ -3,10 +3,12 @@ import { UserService } from './user.service'; import { UserController } from './user.controller'; import { TypeOrmModule } from '@nestjs/typeorm'; import { User } from './entities/user.entity'; +import { MailingModule } from 'src/mailing/mailing.module'; @Module({ imports: [ TypeOrmModule.forFeature([User]), + MailingModule ], providers: [UserService], controllers: [UserController], diff --git a/package-lock.json b/package-lock.json index 5a95e570..4cbcae5a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,7 +43,6 @@ "class-validator": "^0.13.2", "googleapis": "^118.0.0", "handlebars": "^4.7.7", - "nodemailer": "^6.9.3", "passport": "^0.6.0", "passport-jwt": "^4.0.0", "passport-local": "^1.0.0",