Merge pull request #82 from SyncrowIOT/feature/otp-config

Feature/otp config
This commit is contained in:
faris Aljohari
2024-08-21 11:25:10 +03:00
committed by GitHub
2 changed files with 10 additions and 7 deletions

View File

@ -88,3 +88,5 @@ FIREBASE_MEASUREMENT_ID=
FIREBASE_DATABASE_URL= FIREBASE_DATABASE_URL=
OTP_LIMITER=

View File

@ -20,6 +20,7 @@ import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.
import * as argon2 from 'argon2'; import * as argon2 from 'argon2';
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds'; import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
import { LessThan, MoreThan } from 'typeorm'; import { LessThan, MoreThan } from 'typeorm';
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
export class UserAuthService { export class UserAuthService {
@ -32,6 +33,7 @@ export class UserAuthService {
private readonly emailService: EmailService, private readonly emailService: EmailService,
private readonly userRoleRepository: UserRoleRepository, private readonly userRoleRepository: UserRoleRepository,
private readonly roleTypeRepository: RoleTypeRepository, private readonly roleTypeRepository: RoleTypeRepository,
private readonly configService: ConfigService,
) {} ) {}
async signUp(userSignUpDto: UserSignUpDto): Promise<UserEntity> { async signUp(userSignUpDto: UserSignUpDto): Promise<UserEntity> {
@ -96,11 +98,9 @@ export class UserAuthService {
data.password, data.password,
data.regionUuid, data.regionUuid,
); );
if (!user) { if (!user) {
throw new UnauthorizedException('Invalid login credentials.'); throw new UnauthorizedException('Invalid login credentials.');
} }
const session = await Promise.all([ const session = await Promise.all([
await this.sessionRepository.update( await this.sessionRepository.update(
{ userId: user.id }, { userId: user.id },
@ -114,7 +114,6 @@ export class UserAuthService {
isLoggedOut: false, isLoggedOut: false,
}), }),
]); ]);
return await this.authService.login({ return await this.authService.login({
email: user.email, email: user.email,
userId: user.uuid, userId: user.uuid,
@ -142,8 +141,10 @@ export class UserAuthService {
} }
async generateOTP(data: UserOtpDto): Promise<string> { async generateOTP(data: UserOtpDto): Promise<string> {
const threeDaysAgo = new Date(); const otpLimiter = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3); otpLimiter.setDate(
otpLimiter.getDate() - this.configService.get<number>('OTP_LIMITER'),
);
const userExists = await this.userRepository.exists({ const userExists = await this.userRepository.exists({
where: { where: {
region: data.regionUuid region: data.regionUuid
@ -162,14 +163,14 @@ export class UserAuthService {
await this.otpRepository.delete({ await this.otpRepository.delete({
email: data.email, email: data.email,
type: data.type, type: data.type,
createdAt: LessThan(threeDaysAgo), createdAt: LessThan(otpLimiter),
}); });
const countOfOtp = await this.otpRepository.count({ const countOfOtp = await this.otpRepository.count({
withDeleted: true, withDeleted: true,
where: { where: {
email: data.email, email: data.email,
type: data.type, type: data.type,
createdAt: MoreThan(threeDaysAgo), createdAt: MoreThan(otpLimiter),
}, },
}); });
const lastOtp = await this.otpRepository.findOne({ const lastOtp = await this.otpRepository.findOne({