Skip to content

Commit

Permalink
improve email service and add it to the user service.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusVinix committed Jun 21, 2023
1 parent 5b85f3c commit aaec46c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 88 deletions.
1 change: 0 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 7 additions & 41 deletions api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -254,46 +253,13 @@ export class AuthService {
</div>
</div>
`;
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: `
// <div style="width: 100%; height: 100%; font-family: 'Arial'">
// <h3 style="font-size: 30px; margin: 30px; color: black;">
// Hello, ${user.nick}
// </h3>
// <p style="font-size: 25px; margin: 40px auto; color: black; width:627px">
// You have requested to change the password.
// </p>
// <p style="font-size: 25px; margin: 20px auto; color: black; width:257px">
// Your change password code is:
// </p>
// <div style="color: white; font-size: 50px; font-weight: bold;
// margin-top: 40px; padding: 10px 42% 10px;
// border-radius: 20px; background-color: #7C1CED">
// ${sendedCode}
// </div>
// </div>
// `,
// });
const emailDto: EmailDto = {
body: body,
subject: 'Recovery password Code from Transcendence',
emailTo: [user.email as string]
};
await this.mailingService.sendMail(emailDto);
return ;
}

Expand Down
5 changes: 5 additions & 0 deletions api/src/mailing/dto/email.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class EmailDto {
emailTo: string[];
subject: string;
body: string;
}
8 changes: 7 additions & 1 deletion api/src/mailing/mailing.controller.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}

}
11 changes: 6 additions & 5 deletions api/src/mailing/mailing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
66 changes: 27 additions & 39 deletions api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ 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';
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()
Expand Down Expand Up @@ -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 = `
<div style="width: 100%; height: 100%; font-family: 'Arial'">
<h3 style="font-size: 30px; margin: 30px; color: black;">
Hello, ${user.nick}
</h3>
<p style="font-size: 25px; margin: 40px auto; color: black; width:627px">
You have requested to enable Two-Factor Authentication
</p>
<p style="font-size: 25px; margin: 20px auto; color: black; width:257px">
Your activation code is:
</p>
<div style="color: white; font-size: 50px; font-weight: bold;
margin-top: 40px; padding: 10px 42% 10px;
border-radius: 20px; background-color: #7C1CED">
${sendedCode}
</div>
</div>
`;
const emailDto: EmailDto = {
body: body,
subject: 'Verify Code from Transcendence',
text: `Your validation code is '${sendedCode}'`,
html: `
<div style="width: 100%; height: 100%; font-family: 'Arial'">
<h3 style="font-size: 30px; margin: 30px; color: black;">
Hello, ${user.nick}
</h3>
<p style="font-size: 25px; margin: 40px auto; color: black; width:627px">
You have requested to enable Two-Factor Authentication
</p>
<p style="font-size: 25px; margin: 20px auto; color: black; width:257px">
Your activation code is:
</p>
<div style="color: white; font-size: 50px; font-weight: bold;
margin-top: 40px; padding: 10px 42% 10px;
border-radius: 20px; background-color: #7C1CED">
${sendedCode}
</div>
</div>
`,
});
emailTo: [user.email as string]
};
await this.mailingService.sendMail(emailDto);
} else {
throw new InternalServerErrorException('Error: Mail can\'t be empty');
}
Expand Down
2 changes: 2 additions & 0 deletions api/src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aaec46c

Please sign in to comment.