feat: ability to enable/disable email confirmation from env variables

This commit is contained in:
Ahmed Bouhuolia
2024-05-03 18:30:19 +02:00
parent cb88c234d1
commit f4440c9a03
8 changed files with 39 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
import { Service, Inject, Container } from 'typedi';
import { Service, Inject } from 'typedi';
import {
IRegisterDTO,
ISystemUser,
@@ -13,11 +13,6 @@ import { AuthSignupConfirmService } from './AuthSignupConfirm';
import { SystemUser } from '@/system/models';
import { AuthSignupConfirmResend } from './AuthSignupResend';
interface ISignupConfirmDTO {
token: string;
email: string;
}
@Service()
export default class AuthenticationApplication {
@Inject()
@@ -72,13 +67,12 @@ export default class AuthenticationApplication {
}
/**
*
* @param {string} email
* @param {string} token
* @returns
* Resends the confirmation email of the given system user.
* @param {number} userId - System user id.
* @returns {Promise<void>}
*/
public async signUpConfirmSend(email: string, token: string) {
return this.authSignupConfirmService.signUpConfirm(email, token);
public async signUpConfirmResend(userId: number) {
return this.authSignUpConfirmResendService.signUpConfirmResend(userId);
}
/**

View File

@@ -1,4 +1,4 @@
import { isEmpty, omit } from 'lodash';
import { defaultTo, isEmpty, omit } from 'lodash';
import moment from 'moment';
import crypto from 'crypto';
import { ServiceError } from '@/exceptions';
@@ -42,7 +42,13 @@ export class AuthSignupService {
await this.validateEmailUniqiness(signupDTO.email);
const hashedPassword = await hashPassword(signupDTO.password);
const verifyToken = crypto.randomBytes(64).toString('hex');
const verifyTokenCrypto = crypto.randomBytes(64).toString('hex');
const verifiedEnabed = defaultTo(config.signupConfirmation.enabled, false);
const verifyToken = verifiedEnabed ? verifyTokenCrypto : '';
const verified = !verifiedEnabed;
const inviteAcceptedAt = moment().format('YYYY-MM-DD');
// Triggers signin up event.
await this.eventPublisher.emitAsync(events.auth.signingUp, {
@@ -53,10 +59,11 @@ export class AuthSignupService {
const registeredUser = await systemUserRepository.create({
...omit(signupDTO, 'country'),
verifyToken,
verified,
active: true,
password: hashedPassword,
tenantId: tenant.id,
inviteAcceptedAt: moment().format('YYYY-MM-DD'),
inviteAcceptedAt,
});
// Triggers signed up event.
await this.eventPublisher.emitAsync(events.auth.signUp, {

View File

@@ -52,6 +52,6 @@ export class AuthSignupConfirmService {
userId,
} as IAuthSignUpVerifiedEventPayload);
return updatedUser;
return updatedUser as SystemUser;
}
}

View File

@@ -1,6 +1,6 @@
import { Inject, Service } from 'typedi';
import { ServiceError } from '@/exceptions';
import { SystemUser } from '@/system/models';
import { Inject, Service } from 'typedi';
import { ERRORS } from './_constants';
@Service()
@@ -9,18 +9,19 @@ export class AuthSignupConfirmResend {
private agenda: any;
/**
*
* @param {number} tenantId
* @param {string} email
* Resends the email confirmation of the given user.
* @param {number} userId - User ID.
* @returns {Promise<void>}
*/
public async signUpConfirmResend(userId: number) {
const user = await SystemUser.query().findById(userId).throwIfNotFound();
//
// Throw error if the user is already verified.
if (user.verified) {
throw new ServiceError(ERRORS.USER_ALREADY_VERIFIED);
}
if (user.verifyToken) {
// Throw error if the verification token is not exist.
if (!user.verifyToken) {
throw new ServiceError(ERRORS.USER_ALREADY_VERIFIED);
}
const payload = {