Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send email verification issue #823

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions apps/user/src/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,48 +87,52 @@ export class UserService {
* @param userEmailVerification
* @returns
*/

async sendVerificationMail(userEmailVerification: ISendVerificationEmail): Promise<user> {
try {
const { email, brandLogoUrl, platformName } = userEmailVerification;

const { email, brandLogoUrl, platformName, clientId, clientSecret } = userEmailVerification;
if ('PROD' === process.env.PLATFORM_PROFILE_MODE) {
// eslint-disable-next-line prefer-destructuring
const domain = email.split('@')[1];

if (DISALLOWED_EMAIL_DOMAIN.includes(domain)) {
throw new BadRequestException(ResponseMessages.user.error.InvalidEmailDomain);
}
}

const userDetails = await this.userRepository.checkUserExist(email);

if (userDetails?.isEmailVerified) {
throw new ConflictException(ResponseMessages.user.error.exists);
}

if (userDetails && !userDetails.isEmailVerified) {
throw new ConflictException(ResponseMessages.user.error.verificationAlreadySent);
if (userDetails) {
if (userDetails.isEmailVerified) {
throw new ConflictException(ResponseMessages.user.error.exists);
} else {
throw new ConflictException(ResponseMessages.user.error.verificationAlreadySent);
}
}

const verifyCode = uuidv4();
const uniqueUsername = await this.createUsername(email, verifyCode);
userEmailVerification.username = uniqueUsername;
const resUser = await this.userRepository.createUser(userEmailVerification, verifyCode);

const token = await this.clientRegistrationService.getManagementToken(resUser.clientId, resUser.clientSecret);
const getClientData = await this.clientRegistrationService.getClientRedirectUrl(resUser.clientId, token);
let sendVerificationMail: boolean;

try {
const token = await this.clientRegistrationService.getManagementToken(clientId, clientSecret);
const getClientData = await this.clientRegistrationService.getClientRedirectUrl(clientId, token);
const [redirectUrl] = getClientData[0]?.redirectUris || [];

if (!redirectUrl) {
throw new NotFoundException(ResponseMessages.user.error.redirectUrlNotFound);
}

await this.sendEmailForVerification(email, resUser.verificationCode, redirectUrl, resUser.clientId, brandLogoUrl, platformName);
sendVerificationMail = await this.sendEmailForVerification(email, verifyCode, redirectUrl, clientId, brandLogoUrl, platformName);
} catch (error) {
throw new InternalServerErrorException(ResponseMessages.user.error.emailSend);
}

return resUser;

if (sendVerificationMail) {
const uniqueUsername = await this.createUsername(email, verifyCode);
userEmailVerification.username = uniqueUsername;
const resUser = await this.userRepository.createUser(userEmailVerification, verifyCode);
return resUser;
}
} catch (error) {
this.logger.error(`In Create User : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
Expand Down