fix(auth): normalize mobile digits and whitespace in login, register, and admin user creation
Deploy Canina / deploy (push) Successful in 1m43s

This commit is contained in:
parsa aghaei committed 2026-08-16 15:31:28 +03:30
1 parent 2984b9f637
commit e1f471c978
3 files changed
+69 -21

No files matched your search

+12 -5
View File
@@ -7,6 +7,7 @@ import { VerifyOtpDto } from './dto/verify-otp.dto';
import { SmsService } from '../common/services/sms.service';
import * as bcrypt from 'bcryptjs';
import * as crypto from 'crypto';
import { normalizeMobile } from '../common/utils/phone.utils';
export class RegisterInput {
firstName!: string;
@@ -36,7 +37,7 @@ export class AuthService {
) {}
async sendOtp(sendOtpDto: SendOtpDto) {
const { phoneNumber } = sendOtpDto;
const phoneNumber = normalizeMobile(sendOtpDto.phoneNumber);
const code = crypto.randomInt(10000, 100000).toString();
@@ -62,7 +63,8 @@ export class AuthService {
}
async verifyOtp(verifyOtpDto: VerifyOtpDto) {
const { phoneNumber, code } = verifyOtpDto;
const phoneNumber = normalizeMobile(verifyOtpDto.phoneNumber);
const code = String(verifyOtpDto.code).trim();
const savedCode = await this.redisService.get(`otp:${phoneNumber}`);
@@ -102,7 +104,11 @@ export class AuthService {
}
async register(registerDto: RegisterInput) {
const { firstName, lastName, email, mobile, password } = registerDto;
const firstName = registerDto.firstName.trim();
const lastName = registerDto.lastName.trim();
const mobile = normalizeMobile(registerDto.mobile);
const email = registerDto.email ? registerDto.email.trim().toLowerCase() : null;
const password = registerDto.password?.trim();
const existingUser = await this.prisma.user.findUnique({
where: { mobile },
@@ -132,7 +138,7 @@ export class AuthService {
data: {
firstName,
lastName,
email: email || null,
email,
mobile,
password: hashedPassword,
},
@@ -151,7 +157,8 @@ export class AuthService {
}
async login(loginDto: LoginInput) {
const { mobile, password } = loginDto;
const mobile = normalizeMobile(loginDto.mobile);
const password = loginDto.password ? loginDto.password.trim() : '';
const user = await this.prisma.user.findUnique({ where: { mobile } });
if (!user) {