chore: complete utils modules
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { CACHE_MANAGER, Cache } from "@nestjs/cache-manager";
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class CacheService {
|
||||
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
|
||||
|
||||
async get<T>(key: string) {
|
||||
return await this.cacheManager.get<T>(key);
|
||||
}
|
||||
|
||||
async set(key: string, value: string, ttl: number = 120 * 1000) {
|
||||
await this.cacheManager.set(key, value, ttl);
|
||||
}
|
||||
|
||||
async del(key: string) {
|
||||
await this.cacheManager.del(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { randomInt } from "node:crypto";
|
||||
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
|
||||
import { CacheService } from "./cache.service";
|
||||
import { AuthMessage } from "../../../common/enums/message.enum";
|
||||
|
||||
@Injectable()
|
||||
export class OTPService {
|
||||
constructor(private cacheService: CacheService) {}
|
||||
//
|
||||
private generateOTP(): string {
|
||||
return randomInt(10000, 99999).toString();
|
||||
}
|
||||
|
||||
async generateAndStore(identifier: string, cacheKey: string = "Login") {
|
||||
const otp = this.generateOTP();
|
||||
const key = `${identifier}:${cacheKey}:OTP`;
|
||||
await this.cacheService.set(key, otp);
|
||||
}
|
||||
|
||||
async verifyOtp(identifier: string, otpCode: string, cacheKey: string = "Login") {
|
||||
const key = `${identifier}:${cacheKey}:Otp`;
|
||||
const codeInCache = await this.cacheService.get(key);
|
||||
|
||||
if (!codeInCache || otpCode !== codeInCache) throw new BadRequestException(AuthMessage.INVALID_OTP);
|
||||
await this.cacheService.del(key);
|
||||
}
|
||||
|
||||
// async verify(identifier: string, otp: string): Promise<boolean> {
|
||||
// // Here you would typically verify the OTP against stored value
|
||||
// // and check if it's not expired
|
||||
// return true; // Placeholder implementation
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class PasswordService {
|
||||
async hashPassword(rawPassword: string) {}
|
||||
}
|
||||
@@ -1,15 +1,20 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { SMS_CONFIG } from "./constants";
|
||||
import { CacheService } from "./providers/cache.service";
|
||||
import { OTPService } from "./providers/otp.service";
|
||||
import { SmsConfigs } from "../../configs/sms.config";
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
OTPService,
|
||||
CacheService,
|
||||
{
|
||||
provide: SMS_CONFIG,
|
||||
useFactory: SmsConfigs().useFactory,
|
||||
inject: SmsConfigs().inject
|
||||
inject: SmsConfigs().inject,
|
||||
},
|
||||
],
|
||||
exports: [SMS_CONFIG]
|
||||
exports: [SMS_CONFIG],
|
||||
})
|
||||
export class UtilsModule { }
|
||||
export class UtilsModule {}
|
||||
|
||||
Reference in New Issue
Block a user