otp in database
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { Injectable, BadRequestException } from '@nestjs/common';
|
||||
import { RequestOtpDto } from '../dto/request-otp.dto';
|
||||
import { CacheService } from '../../util/cache.service';
|
||||
import { SmsService } from '../../notification/services/sms.service';
|
||||
import { UserService } from '../../user/providers/user.service';
|
||||
import { randomInt } from 'crypto';
|
||||
@@ -9,46 +8,35 @@ import { AuthMessage } from 'src/common/enums/message.enum';
|
||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||
import { AdminLoginTransformer } from '../transformers/admin-login.transformer';
|
||||
import { UserLoginTransformer } from '../transformers/user-login.transformer';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { normalizePhone } from '../../util/phone.util';
|
||||
import { OtpService } from './otp.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
readonly ADMIN_PERMISSIONS_KEY = 'admin-perms';
|
||||
private OTP_EXPIRATION_TIME: number;
|
||||
|
||||
constructor(
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly smsService: SmsService,
|
||||
private readonly userService: UserService,
|
||||
private readonly tokensService: TokensService,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly otpService: OtpService,
|
||||
) {
|
||||
this.OTP_EXPIRATION_TIME = this.configService.get<number>('OTP_EXPIRATION_TIME') ?? 240;
|
||||
}
|
||||
|
||||
private userOtpKey(phone: string) {
|
||||
return `otp:${phone}`;
|
||||
}
|
||||
|
||||
private adminOtpKey(phone: string) {
|
||||
return `otp-admin:${phone}`;
|
||||
|
||||
}
|
||||
|
||||
async requestOtp(dto: RequestOtpDto, isAdmin: boolean) {
|
||||
const { phone } = dto;
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
|
||||
|
||||
if (isAdmin) {
|
||||
await this.valdateAdmin(normalizedPhone)
|
||||
}
|
||||
|
||||
const code = this.generateOtpCode();
|
||||
const key = isAdmin ? this.adminOtpKey(normalizedPhone) : this.userOtpKey(normalizedPhone);
|
||||
|
||||
await this.cacheService.set(key, code, this.OTP_EXPIRATION_TIME);
|
||||
|
||||
|
||||
await this.otpService.set(normalizedPhone, code)
|
||||
|
||||
// await this.smsService.sendotp(normalizedPhone, code);
|
||||
|
||||
return { code };
|
||||
@@ -56,16 +44,14 @@ export class AuthService {
|
||||
|
||||
async verifyOtp(phone: string, code: string) {
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
const key = this.userOtpKey(normalizedPhone);
|
||||
const cachedCode = await this.cacheService.get(key);
|
||||
|
||||
const cachedCode = await this.otpService.get(normalizedPhone);
|
||||
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
|
||||
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
|
||||
|
||||
await this.cacheService.del(key);
|
||||
await this.otpService.delete(normalizedPhone)
|
||||
|
||||
const user = await this.userService.getOrCreate({
|
||||
firstName: '[کاربر]',
|
||||
lastName: '',
|
||||
phone: normalizedPhone,
|
||||
gender: true,
|
||||
maxCredit: 0
|
||||
@@ -81,13 +67,13 @@ export class AuthService {
|
||||
|
||||
async verifyOtpAdmin(phone: string, code: string) {
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
const key = this.adminOtpKey(normalizedPhone);
|
||||
const cachedCode = await this.cacheService.get(key);
|
||||
|
||||
const cachedCode = await this.otpService.get(normalizedPhone);
|
||||
|
||||
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
|
||||
|
||||
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
|
||||
await this.cacheService.del(key);
|
||||
await this.otpService.delete(normalizedPhone);
|
||||
|
||||
const admin = await this.adminRepository.findOne({ phone: normalizedPhone }, { populate: ['role', 'role.permissions'] });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user