This commit is contained in:
2026-02-25 16:58:36 +03:30
parent 5112ff25ae
commit cf7fd04913
14 changed files with 68 additions and 117 deletions
+4 -1
View File
@@ -1,10 +1,13 @@
import { IsNotEmpty, IsString, IsMobilePhone } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { normalizePhoneToLocal } from 'src/modules/util/phone.util';
import { Transform } from 'class-transformer';
export class RequestOtpDto {
@Transform(({ value }) => normalizePhoneToLocal(value))
@IsNotEmpty()
@IsString()
@ApiProperty({ example: '09362532122', description: 'Mobile number' })
@ApiProperty({ example: '09362532122', description: 'Mobile number (accepts +98, 0098, 98, 09 formats)' })
@IsMobilePhone('fa-IR')
phone: string;
+4 -1
View File
@@ -1,10 +1,13 @@
import { IsNotEmpty, IsString, Length, IsMobilePhone } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { normalizePhoneToLocal } from 'src/modules/util/phone.util';
export class VerifyOtpDto {
@Transform(({ value }) => normalizePhoneToLocal(value))
@IsNotEmpty()
@IsString()
@ApiProperty({ example: '09362532122', description: 'Mobile number' })
@ApiProperty({ example: '09362532122', description: 'Mobile number (accepts +98, 0098, 98, 09 formats)' })
@IsMobilePhone('fa-IR')
phone: string;
+8 -12
View File
@@ -8,7 +8,6 @@ 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 { normalizePhone } from '../../util/phone.util';
import { OtpService } from './otp.service';
@Injectable()
@@ -27,15 +26,14 @@ export class AuthService {
async requestOtp(dto: RequestOtpDto, isAdmin: boolean) {
const { phone } = dto;
const normalizedPhone = normalizePhone(phone);
if (isAdmin) {
await this.valdateAdmin(normalizedPhone)
await this.valdateAdmin(phone)
}
const code = this.generateOtpCode();
await this.otpService.set(normalizedPhone, code)
await this.otpService.set(phone, code)
// await this.smsService.sendotp(normalizedPhone, code);
@@ -43,16 +41,15 @@ export class AuthService {
}
async verifyOtp(phone: string, code: string) {
const normalizedPhone = normalizePhone(phone);
const cachedCode = await this.otpService.get(normalizedPhone);
const cachedCode = await this.otpService.get(phone);
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
await this.otpService.delete(normalizedPhone)
await this.otpService.delete(phone)
const user = await this.userService.getOrCreate({
phone: normalizedPhone,
phone: phone,
gender: true,
// maxCredit: 0
});
@@ -66,16 +63,15 @@ export class AuthService {
}
async verifyOtpAdmin(phone: string, code: string) {
const normalizedPhone = normalizePhone(phone);
const cachedCode = await this.otpService.get(normalizedPhone);
const cachedCode = await this.otpService.get(phone);
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
await this.otpService.delete(normalizedPhone);
await this.otpService.delete(phone);
const admin = await this.adminRepository.findOne({ phone: normalizedPhone }, { populate: ['role', 'role.permissions'] });
const admin = await this.adminRepository.findOne({ phone }, { populate: ['role', 'role.permissions'] });
if (!admin) {
throw new BadRequestException(AuthMessage.ADMIN_NOT_FOUND);