feat: connect UI texts, address CRUD, OTP autofocus and profile to backend

This commit is contained in:
parsa committed 2026-05-26 21:39:14 +03:30
1 parent 4e2c8d5cfe
commit 467532713c
82 files changed
+14436 -274

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { AuthService } from './auth.service';
import { SendOtpDto } from './dto/send-otp.dto';
import { VerifyOtpDto } from './dto/verify-otp.dto';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
@ApiTags('Auth')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('send-otp')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'ارسال کد تایید پیامکی' })
@ApiResponse({ status: 200, description: 'کد با موفقیت ارسال شد' })
sendOtp(@Body() sendOtpDto: SendOtpDto) {
return this.authService.sendOtp(sendOtpDto);
}
@Post('verify-otp')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'تایید کد پیامکی و ورود/ثبت‌نام' })
@ApiResponse({ status: 200, description: 'ورود موفق به همراه توکن' })
verifyOtp(@Body() verifyOtpDto: VerifyOtpDto) {
return this.authService.verifyOtp(verifyOtpDto);
}
}
+22
View File
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { JwtStrategy } from './jwt.strategy';
import { UsersModule } from '../users/users.module';
@Module({
imports: [
UsersModule,
PassportModule,
JwtModule.register({
secret: process.env.JWT_SECRET || 'super-secret-key',
signOptions: { expiresIn: '7d' },
}),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
exports: [AuthService],
})
export class AuthModule {}
+66
View File
@@ -0,0 +1,66 @@
import { Injectable, BadRequestException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { PrismaService } from '../prisma/prisma.service';
import { RedisService } from '../redis/redis.service';
import { SendOtpDto } from './dto/send-otp.dto';
import { VerifyOtpDto } from './dto/verify-otp.dto';
@Injectable()
export class AuthService {
constructor(
private prisma: PrismaService,
private jwtService: JwtService,
private redisService: RedisService,
) {}
async sendOtp(sendOtpDto: SendOtpDto) {
const { phoneNumber } = sendOtpDto;
const code = Math.floor(10000 + Math.random() * 90000).toString();
console.log(`[Mock SMS] Sending OTP ${code} to ${phoneNumber}`);
await this.redisService.set(`otp:${phoneNumber}`, code, 120);
return { success: true, message: 'کد تایید ارسال شد', code };
}
async verifyOtp(verifyOtpDto: VerifyOtpDto) {
const { phoneNumber, code } = verifyOtpDto;
const savedCode = await this.redisService.get(`otp:${phoneNumber}`);
if (!savedCode) {
throw new BadRequestException({ message: 'کد تایید منقضی شده است', error: 'OTP_EXPIRED' });
}
if (savedCode !== code) {
throw new BadRequestException({ message: 'کد تایید اشتباه است', error: 'OTP_INVALID' });
}
await this.redisService.del(`otp:${phoneNumber}`);
let user = await this.prisma.user.findUnique({ where: { mobile: phoneNumber } });
if (!user) {
user = await this.prisma.user.create({
data: {
mobile: phoneNumber,
firstName: 'کاربر',
lastName: 'جدید',
email: `${phoneNumber}@temp.local`,
},
});
}
const payload = { sub: user.id, phoneNumber: user.mobile };
const accessToken = this.jwtService.sign(payload);
return {
success: true,
data: {
user,
accessToken,
},
};
}
}
+10
View File
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, Matches } from 'class-validator';
export class SendOtpDto {
@ApiProperty({ description: 'شماره موبایل کاربر', example: '09123456789' })
@IsNotEmpty()
@IsString()
@Matches(/^09\d{9}$/, { message: 'شماره موبایل نامعتبر است' })
phoneNumber: string;
}
+16
View File
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, Length, Matches } from 'class-validator';
export class VerifyOtpDto {
@ApiProperty({ description: 'شماره موبایل کاربر', example: '09123456789' })
@IsNotEmpty()
@IsString()
@Matches(/^09\d{9}$/, { message: 'شماره موبایل نامعتبر است' })
phoneNumber: string;
@ApiProperty({ description: 'کد تایید پیامک شده', example: '12345' })
@IsNotEmpty()
@IsString()
@Length(5, 5, { message: 'کد تایید باید ۵ رقم باشد' })
code: string;
}
+12
View File
@@ -0,0 +1,12 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
handleRequest(err: any, user: any, info: any) {
if (err || !user) {
throw err || new UnauthorizedException('لطفا ابتدا وارد حساب کاربری خود شوید');
}
return user;
}
}
+23
View File
@@ -0,0 +1,23 @@
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UsersService } from '../users/users.service';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly usersService: UsersService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: process.env.JWT_SECRET || 'super-secret-key',
});
}
async validate(payload: any) {
const user = await this.usersService.findById(payload.sub);
if (!user) {
throw new UnauthorizedException('کاربر یافت نشد');
}
return user;
}
}