fix(auth): normalize mobile digits and whitespace in login, register, and admin user creation
Deploy Canina / deploy (push) Successful in 1m43s
Deploy Canina / deploy (push) Successful in 1m43s
This commit is contained in:
1 parent
2984b9f637
commit
e1f471c978
3 files changed
+69
-21
No files matched your search
@@ -9,6 +9,7 @@ import { PrismaService } from '../prisma/prisma.service';
|
||||
import { RedisService } from '../redis/redis.service';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { CreateUserDto, UpdateUserDto } from './dto/user.dto';
|
||||
import { normalizeMobile } from '../common/utils/phone.utils';
|
||||
|
||||
export class PaginationQuery {
|
||||
page?: number | string;
|
||||
@@ -177,8 +178,9 @@ export class AdminService {
|
||||
}
|
||||
|
||||
async createUser(dto: CreateUserDto) {
|
||||
const mobile = normalizeMobile(dto.mobile);
|
||||
const existingMobile = await this.prisma.user.findUnique({
|
||||
where: { mobile: dto.mobile },
|
||||
where: { mobile },
|
||||
});
|
||||
if (existingMobile) {
|
||||
throw new BadRequestException('کاربری با این شماره موبایل قبلاً در سیستم ثبت شده است');
|
||||
@@ -186,21 +188,22 @@ export class AdminService {
|
||||
|
||||
if (dto.email) {
|
||||
const existingEmail = await this.prisma.user.findUnique({
|
||||
where: { email: dto.email },
|
||||
where: { email: dto.email.trim().toLowerCase() },
|
||||
});
|
||||
if (existingEmail) {
|
||||
throw new BadRequestException('کاربری با این آدرس ایمیل قبلاً در سیستم ثبت شده است');
|
||||
}
|
||||
}
|
||||
|
||||
const hashedPassword = dto.password ? await bcrypt.hash(dto.password, 10) : '';
|
||||
const passwordToHash = dto.password?.trim();
|
||||
const hashedPassword = passwordToHash ? await bcrypt.hash(passwordToHash, 10) : '';
|
||||
|
||||
return this.prisma.user.create({
|
||||
data: {
|
||||
firstName: dto.firstName,
|
||||
lastName: dto.lastName,
|
||||
mobile: dto.mobile,
|
||||
email: dto.email || null,
|
||||
firstName: dto.firstName.trim(),
|
||||
lastName: dto.lastName.trim(),
|
||||
mobile,
|
||||
email: dto.email ? dto.email.trim().toLowerCase() : null,
|
||||
password: hashedPassword,
|
||||
role: dto.role || 'User_PetOwner',
|
||||
walletBalance:
|
||||
@@ -221,18 +224,20 @@ export class AdminService {
|
||||
throw new NotFoundException(`کاربری با شناسه ${id} یافت نشد`);
|
||||
}
|
||||
|
||||
if (dto.mobile && dto.mobile !== user.mobile) {
|
||||
const normalizedMobile = dto.mobile ? normalizeMobile(dto.mobile) : undefined;
|
||||
if (normalizedMobile && normalizedMobile !== user.mobile) {
|
||||
const existingMobile = await this.prisma.user.findUnique({
|
||||
where: { mobile: dto.mobile },
|
||||
where: { mobile: normalizedMobile },
|
||||
});
|
||||
if (existingMobile && existingMobile.id !== id) {
|
||||
throw new BadRequestException('این شماره موبایل متعلق به کاربر دیگری است');
|
||||
}
|
||||
}
|
||||
|
||||
if (dto.email && dto.email !== user.email) {
|
||||
const normalizedEmail = dto.email ? dto.email.trim().toLowerCase() : undefined;
|
||||
if (normalizedEmail && normalizedEmail !== user.email) {
|
||||
const existingEmail = await this.prisma.user.findUnique({
|
||||
where: { email: dto.email },
|
||||
where: { email: normalizedEmail },
|
||||
});
|
||||
if (existingEmail && existingEmail.id !== id) {
|
||||
throw new BadRequestException('این آدرس ایمیل متعلق به کاربر دیگری است');
|
||||
@@ -240,16 +245,16 @@ export class AdminService {
|
||||
}
|
||||
|
||||
const updateData: Prisma.UserUpdateInput = {};
|
||||
if (dto.firstName !== undefined) updateData.firstName = dto.firstName;
|
||||
if (dto.lastName !== undefined) updateData.lastName = dto.lastName;
|
||||
if (dto.mobile !== undefined) updateData.mobile = dto.mobile;
|
||||
if (dto.email !== undefined) updateData.email = dto.email || null;
|
||||
if (dto.firstName !== undefined) updateData.firstName = dto.firstName.trim();
|
||||
if (dto.lastName !== undefined) updateData.lastName = dto.lastName.trim();
|
||||
if (normalizedMobile !== undefined) updateData.mobile = normalizedMobile;
|
||||
if (normalizedEmail !== undefined) updateData.email = normalizedEmail || null;
|
||||
if (dto.role !== undefined) updateData.role = dto.role;
|
||||
if (dto.walletBalance !== undefined) {
|
||||
updateData.walletBalance = new Prisma.Decimal(dto.walletBalance);
|
||||
}
|
||||
if (dto.password && dto.password.trim().length >= 6) {
|
||||
updateData.password = await bcrypt.hash(dto.password, 10);
|
||||
updateData.password = await bcrypt.hash(dto.password.trim(), 10);
|
||||
}
|
||||
|
||||
return this.prisma.user.update({
|
||||
|
||||
Reference in new issue
Block a user