feat(auth): implement WebOTP SMS auto-fill, auto-submit, password setup in dashboard, and password visibility toggle
This commit is contained in:
1 parent
897b4d61ec
commit
cbd7e1a24d
6 files changed
+646
-265
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString, IsEmail } from 'class-validator';
|
||||
import { IsOptional, IsString, IsEmail, MinLength } from 'class-validator';
|
||||
|
||||
export class UpdateProfileDto {
|
||||
@ApiPropertyOptional({ description: 'نام کاربر' })
|
||||
@@ -21,4 +21,15 @@ export class UpdateProfileDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobile?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'رمز عبور جدید' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(6, { message: 'رمز عبور باید حداقل ۶ کاراکتر باشد' })
|
||||
password?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'رمز عبور فعلی (در صورت وجود)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
currentPassword?: string;
|
||||
}
|
||||
@@ -1,6 +1,13 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
BadRequestException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { normalizeMobile } from '../common/utils/phone.utils';
|
||||
import { UpdateProfileDto } from './dto/update-profile.dto';
|
||||
|
||||
export interface UserAddressInput {
|
||||
title: string;
|
||||
@@ -49,7 +56,13 @@ export class UsersService {
|
||||
},
|
||||
});
|
||||
|
||||
if (dbUser) return dbUser;
|
||||
if (dbUser) {
|
||||
const { password, ...safeUser } = dbUser;
|
||||
return {
|
||||
...safeUser,
|
||||
hasPassword: !!password,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id: '12345678-1234-1234-1234-123456789012',
|
||||
@@ -64,12 +77,13 @@ export class UsersService {
|
||||
orders: [],
|
||||
addresses: [],
|
||||
walletTransactions: [],
|
||||
hasPassword: true,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
return this.prisma.user.findUnique({
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
pets: {
|
||||
@@ -98,12 +112,70 @@ export class UsersService {
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) return null;
|
||||
|
||||
const { password, ...safeUser } = user;
|
||||
return {
|
||||
...safeUser,
|
||||
hasPassword: !!password,
|
||||
};
|
||||
}
|
||||
|
||||
async update(id: string, data: Prisma.UserUpdateInput) {
|
||||
return this.prisma.user.update({
|
||||
async update(id: string, dto: UpdateProfileDto) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (!user) {
|
||||
throw new NotFoundException('کاربر یافت نشد');
|
||||
}
|
||||
|
||||
const updateData: Prisma.UserUpdateInput = {};
|
||||
|
||||
if (dto.firstName !== undefined) updateData.firstName = dto.firstName.trim();
|
||||
if (dto.lastName !== undefined) updateData.lastName = dto.lastName.trim();
|
||||
|
||||
if (dto.mobile !== undefined) {
|
||||
const normalizedMobile = normalizeMobile(dto.mobile);
|
||||
if (normalizedMobile && normalizedMobile !== user.mobile) {
|
||||
const existing = await this.prisma.user.findUnique({
|
||||
where: { mobile: normalizedMobile },
|
||||
});
|
||||
if (existing && existing.id !== id) {
|
||||
throw new BadRequestException('این شماره موبایل قبلاً توسط کاربر دیگری ثبت شده است');
|
||||
}
|
||||
updateData.mobile = normalizedMobile;
|
||||
}
|
||||
}
|
||||
|
||||
if (dto.email !== undefined) {
|
||||
const normalizedEmail = dto.email ? dto.email.trim().toLowerCase() : null;
|
||||
if (normalizedEmail && normalizedEmail !== user.email) {
|
||||
const existing = await this.prisma.user.findUnique({
|
||||
where: { email: normalizedEmail },
|
||||
});
|
||||
if (existing && existing.id !== id) {
|
||||
throw new BadRequestException('این آدرس ایمیل قبلاً توسط کاربر دیگری ثبت شده است');
|
||||
}
|
||||
}
|
||||
updateData.email = normalizedEmail;
|
||||
}
|
||||
|
||||
if (dto.password && dto.password.trim().length >= 6) {
|
||||
// If user already has a password set, require and verify currentPassword
|
||||
if (user.password) {
|
||||
if (!dto.currentPassword) {
|
||||
throw new BadRequestException('برای تغییر رمز عبور، وارد کردن رمز عبور فعلی الزامی است');
|
||||
}
|
||||
const isCurrentMatch = await bcrypt.compare(dto.currentPassword.trim(), user.password);
|
||||
if (!isCurrentMatch) {
|
||||
throw new BadRequestException('رمز عبور فعلی اشتباه است');
|
||||
}
|
||||
}
|
||||
updateData.password = await bcrypt.hash(dto.password.trim(), 10);
|
||||
}
|
||||
|
||||
const updatedUser = await this.prisma.user.update({
|
||||
where: { id },
|
||||
data,
|
||||
data: updateData,
|
||||
include: {
|
||||
pets: true,
|
||||
orders: {
|
||||
@@ -117,6 +189,12 @@ export class UsersService {
|
||||
walletTransactions: true,
|
||||
},
|
||||
});
|
||||
|
||||
const { password, ...safeUser } = updatedUser;
|
||||
return {
|
||||
...safeUser,
|
||||
hasPassword: !!password,
|
||||
};
|
||||
}
|
||||
|
||||
async addAddress(userId: string, data: UserAddressInput) {
|
||||
|
||||
Reference in new issue
Block a user