normalize phone

This commit is contained in:
2025-12-06 22:42:26 +03:30
parent 3978065400
commit 01631c147a
9 changed files with 100 additions and 27 deletions
+10 -6
View File
@@ -11,6 +11,7 @@ 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 '../../utils/phone.util';
@Injectable()
export class AuthService {
@@ -36,24 +37,26 @@ export class AuthService {
async requestOtp(dto: RequestOtpDto, isAdmin: boolean) {
const { phone, slug } = dto;
const normalizedPhone = normalizePhone(phone);
const code = this.generateOtpCode();
const key = isAdmin ? this.adminOtpKey(slug, phone) : this.userOtpKey(slug, phone);
const key = isAdmin ? this.adminOtpKey(slug, normalizedPhone) : this.userOtpKey(slug, normalizedPhone);
await this.cacheService.set(key, code, this.OTP_EXPIRATION_TIME);
// await this.smsService.sendOtp(phone, code);
// await this.smsService.sendOtp(normalizedPhone, code);
return { code };
}
async verifyOtp(phone: string, slug: string, code: string) {
const key = this.userOtpKey(slug, phone);
const normalizedPhone = normalizePhone(phone);
const key = this.userOtpKey(slug, normalizedPhone);
const cachedCode = await this.cacheService.get(key);
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
await this.cacheService.del(key);
const user = await this.userService.findOrCreateByPhone(phone);
const user = await this.userService.findOrCreateByPhone(normalizedPhone);
const rest = await this.restRepository.findOne({ slug });
@@ -69,7 +72,8 @@ export class AuthService {
}
async verifyOtpAdmin(phone: string, slug: string, code: string) {
const key = this.adminOtpKey(slug, phone);
const normalizedPhone = normalizePhone(phone);
const key = this.adminOtpKey(slug, normalizedPhone);
const cachedCode = await this.cacheService.get(key);
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
@@ -77,7 +81,7 @@ export class AuthService {
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
await this.cacheService.del(key);
const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(phone, slug);
const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(normalizedPhone, slug);
if (!admin) {
throw new BadRequestException(AuthMessage.ADMIN_NOT_FOUND);