Skip to content
Snippets Groups Projects
Commit 184dfe44 authored by Jérémie BRISON's avatar Jérémie BRISON
Browse files

fix(changeEmail) : add condition when email already used by an other account

parent 465c212c
No related branches found
No related tags found
3 merge requests!27Recette,!26Dev,!9Feat/change email
......@@ -136,19 +136,24 @@ export class UsersService {
public async changeUserEmail(newEmail: string, oldEmail: string) {
const user = await this.findOne(oldEmail);
const alreadyUsed = await this.findOne(newEmail);
if (user) {
const config = this.mailerService.config;
const ejsPath = this.mailerService.getTemplateLocation(config.templates.changeEmail.ejs);
const jsonConfig = this.mailerService.loadJsonConfig(config.templates.changeEmail.json);
const token = crypto.randomBytes(64).toString('hex');
const html = await ejs.renderFile(ejsPath, {
config,
token: token,
});
this.mailerService.send(user.email, jsonConfig.subject, html);
user.changeEmailToken = token;
user.newEmail = newEmail;
user.save();
if (!alreadyUsed) {
const config = this.mailerService.config;
const ejsPath = this.mailerService.getTemplateLocation(config.templates.changeEmail.ejs);
const jsonConfig = this.mailerService.loadJsonConfig(config.templates.changeEmail.json);
const token = crypto.randomBytes(64).toString('hex');
const html = await ejs.renderFile(ejsPath, {
config,
token: token,
});
this.mailerService.send(user.email, jsonConfig.subject, html);
user.changeEmailToken = token;
user.newEmail = newEmail;
user.save();
return user;
}
throw new HttpException('Email already used', HttpStatus.NOT_ACCEPTABLE);
}
throw new HttpException('Email sent if account exist', HttpStatus.OK);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment