change otp
This commit is contained in:
@@ -11,10 +11,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { AdminAuthGuard } from './guards/adminAuth.guard';
|
||||
import { RefreshToken } from './entities/refresh-token.entity';
|
||||
import { NotificationsModule } from '../notification/notifications.module';
|
||||
import { OtpRepository } from './repository/otp.repository';
|
||||
import { OtpService } from './services/otp.service';
|
||||
import { Otp } from './entities/otp.entity';
|
||||
import { AuthCrone } from './crone/auth.crone';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -35,13 +32,12 @@ import { AuthCrone } from './crone/auth.crone';
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
forwardRef(() => AdminModule),
|
||||
|
||||
MikroOrmModule.forFeature([RefreshToken, Otp]),
|
||||
MikroOrmModule.forFeature([RefreshToken]),
|
||||
forwardRef(() => NotificationsModule),
|
||||
|
||||
UtilsModule
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, TokensService, AdminAuthGuard, OtpRepository, OtpService, AuthCrone],
|
||||
providers: [AuthService, TokensService, AdminAuthGuard, OtpService],
|
||||
exports: [AdminAuthGuard],
|
||||
})
|
||||
export class AuthModule { }
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron } from '@nestjs/schedule';
|
||||
import { OtpRepository } from '../repository/otp.repository';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AuthCrone {
|
||||
private readonly logger = new Logger(AuthCrone.name);
|
||||
|
||||
constructor(
|
||||
private readonly otpRepository: OtpRepository
|
||||
) { }
|
||||
|
||||
@Cron('*/59 * * * *', {
|
||||
name: 'completeOldDeliveredOrders',
|
||||
timeZone: 'UTC',
|
||||
})
|
||||
async removeExpireOtp() {
|
||||
this.logger.log("Delete expire otps")
|
||||
await this.otpRepository.nativeDelete({
|
||||
expiresAt: { $lt: new Date() }
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { Entity, Index, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core';
|
||||
|
||||
@Entity({ tableName: 'otp' })
|
||||
@Index({ properties: ['phone'] })
|
||||
export class Otp {
|
||||
[OptionalProps]?: 'createdAt'
|
||||
|
||||
@PrimaryKey({ type: 'varchar', })
|
||||
phone!: string;
|
||||
|
||||
@Property()
|
||||
code!: string;
|
||||
|
||||
@Property({ columnType: 'timestamptz' })
|
||||
expiresAt!: Date
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Otp } from '../entities/otp.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class OtpRepository extends EntityRepository<Otp> {
|
||||
constructor(
|
||||
readonly em: EntityManager,
|
||||
) {
|
||||
super(em, Otp);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +1,39 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { OtpRepository } from "../repository/otp.repository";
|
||||
import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { CacheService } from "src/modules/util/cache.service";
|
||||
|
||||
@Injectable()
|
||||
export class OtpService {
|
||||
private OTP_EXPIRATION_TIME: number;// in seconds
|
||||
constructor(
|
||||
private readonly otpRepository: OtpRepository,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly em: EntityManager
|
||||
private readonly cacheService: CacheService
|
||||
) {
|
||||
this.OTP_EXPIRATION_TIME = this.configService.get<number>('OTP_EXPIRATION_TIME') ?? 240;
|
||||
}
|
||||
|
||||
async set(phone: string, code: string) {
|
||||
let otpRecord = await this.otpRepository.findOne({ phone })
|
||||
const expiresAt = new Date(Date.now() + this.OTP_EXPIRATION_TIME * 1000)
|
||||
const key = `otp:${phone}`
|
||||
|
||||
if (!otpRecord) {
|
||||
otpRecord = this.otpRepository.create({
|
||||
phone,
|
||||
code,
|
||||
expiresAt
|
||||
})
|
||||
} else {
|
||||
otpRecord.code = code
|
||||
otpRecord.expiresAt = expiresAt
|
||||
}
|
||||
|
||||
await this.em.persistAndFlush(otpRecord)
|
||||
await this.cacheService.set(key, code, this.OTP_EXPIRATION_TIME)
|
||||
|
||||
return { successs: true }
|
||||
|
||||
}
|
||||
|
||||
async get(phone: string) {
|
||||
|
||||
let otpRecord = await this.otpRepository.findOne({ phone, expiresAt: { $gt: new Date() } })
|
||||
|
||||
return otpRecord?.code
|
||||
const key = `otp:${phone}`
|
||||
|
||||
return this.cacheService.get(key)
|
||||
|
||||
}
|
||||
|
||||
async delete(phone: string) {
|
||||
await this.otpRepository.nativeDelete({ phone })
|
||||
const key = `otp:${phone}`
|
||||
|
||||
await this.cacheService.del(key)
|
||||
|
||||
return { successs: true }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user