feat: add simple login logic
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { OTP } from '../otp.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class OtpService {
|
||||
constructor(
|
||||
/**
|
||||
* Inject OTP Repository
|
||||
*/
|
||||
@InjectRepository(OTP)
|
||||
private readonly otpRepository: Repository<OTP>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Generate OTP 5-digit code
|
||||
*/
|
||||
private generateOTPCode(): string {
|
||||
return Math.floor(10_000 + Math.random() * 90_000).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new OTP record
|
||||
*/
|
||||
public async createOTP(phone: string): Promise<string> {
|
||||
// OTP request Rate limit
|
||||
const lastOTP = await this.otpRepository.findOne({
|
||||
where: { phone },
|
||||
order: { createdAt: 'DESC' },
|
||||
});
|
||||
|
||||
if (lastOTP && Date.now() - lastOTP.createdAt.getTime() < 60_000) {
|
||||
// throw new TooMany();
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user