Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
chore: add password retrieval implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca1313 committed Jan 28, 2024
1 parent e066944 commit 309ca1e
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions api-service/api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Request, Response, NextFunction } from "express";
import { StatusCodes } from "http-status-codes";
import {BadRequestError, ErrorTypes, NotFoundError, UnauthorizedError, User} from "core-components";
import { ac } from "../configs/accesscontrol.config";
import GrpcClientPool from "../blockchain/grpc.client.pool";
import {Org2Peer} from "../blockchain/peer.enum";
import mailer from "../configs/mailer.config";
import transformHyperledgerError from "../blockchain/errors/error.handling";


/**
Expand Down Expand Up @@ -162,3 +166,68 @@ export async function deleteProfile(req: Request, res: Response, next: NextFunct
}
return next();
}

function generateRandomPassword(regexPattern: RegExp, length: number): string {
const validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
let password = '';

for (let i = 0; i < length; i++) {
let randomChar = validChars.charAt(Math.floor(Math.random() * validChars.length));
password += randomChar;
}

return password;
}

function getRandomNumber(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

// TODO check if works
/**
* Send user a new password via mail
*
* @param req
* @param res
* @param next
*/
export async function passwordForgotten(req: Request, res: Response, next: NextFunction) {
const regexPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])/; // Example pattern: Alphanumeric characters only
const passwordLength = getRandomNumber(10, 16);
let password = '';
while (!regexPattern.test(password)) {
password = generateRandomPassword(regexPattern, passwordLength);
}

const data = {'password': password}

try {
await User.updateOne({ email: req.body.email }, data);
res.locals.code = StatusCodes.OK;
res.locals.data = true;
} catch(error) {
return next(error);
}

const message = {
from: 'ChainVote',
to: req.body.email,
subject: 'Your new password has been created.',
html: `
Hello ${res.locals.user.firstName} ${res.locals.user.secondName},<br>
This is the new password: <b>${password}</b>
`
};

mailer.sendMail(message).then((info) => {
res.locals.code = 201
res.locals.data = {
msg: "Email sent",
info: info.messageId
}
}).catch((err) => {
return next(err);
}
);
return next();
}

0 comments on commit 309ca1e

Please sign in to comment.