Files
CRM_Montzar/apps/api/src/auth/auth.controller.ts
T

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2026-09-15 14:10:52 +03:30
import {
Body,
Controller,
Delete,
Get,
Patch,
Post,
StreamableFile,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { memoryStorage } from 'multer';
import { AuthService } from './auth.service';
import {
LoginDto,
LoginOtpRequestDto,
LoginOtpVerifyDto,
RegisterOrganizationDto,
RegisterVerifyOtpDto,
2026-09-15 14:10:52 +03:30
UpdateProfileDto,
} from './dto';
import { JwtAuthGuard } from './jwt-auth.guard';
import { CurrentUser } from './current-user.decorator';
2026-09-15 14:10:52 +03:30
type AuthActor = { userId: string; organizationId: string };
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('register')
register(@Body() dto: RegisterOrganizationDto) {
return this.authService.startRegister(dto);
}
@Post('register/verify')
registerVerify(@Body() dto: RegisterVerifyOtpDto) {
return this.authService.verifyRegister(dto);
}
/** ورود با رمز (بدون OTP) */
@Post('login')
login(@Body() dto: LoginDto) {
return this.authService.loginWithPassword(dto);
}
/** درخواست کد ورود با موبایل */
@Post('login/otp')
loginOtpRequest(@Body() dto: LoginOtpRequestDto) {
return this.authService.requestLoginOtp(dto);
}
/** تأیید کد ورود با موبایل */
@Post('login/otp/verify')
loginOtpVerify(@Body() dto: LoginOtpVerifyDto) {
return this.authService.verifyLoginOtp(dto);
}
@UseGuards(JwtAuthGuard)
@Get('me')
2026-09-15 14:10:52 +03:30
me(@CurrentUser() user: AuthActor) {
return this.authService.me(user.userId);
}
2026-09-15 14:10:52 +03:30
@UseGuards(JwtAuthGuard)
@Patch('me')
updateMe(@CurrentUser() user: AuthActor, @Body() dto: UpdateProfileDto) {
return this.authService.updateMe(user.userId, dto);
}
/** عکس پروفایل کاربر جاری */
@UseGuards(JwtAuthGuard)
@Get('me/avatar')
async avatar(@CurrentUser() user: AuthActor) {
const file = await this.authService.openAvatar(user.userId);
return new StreamableFile(file.stream, { type: file.mimeType });
}
@UseGuards(JwtAuthGuard)
@Post('me/avatar')
@UseInterceptors(
FileInterceptor('file', {
storage: memoryStorage(),
limits: { fileSize: 2 * 1024 * 1024 },
}),
)
uploadAvatar(
@CurrentUser() user: AuthActor,
@UploadedFile() file: Express.Multer.File,
) {
return this.authService.saveAvatar(user.userId, user.organizationId, file);
}
@UseGuards(JwtAuthGuard)
@Delete('me/avatar')
removeAvatar(@CurrentUser() user: AuthActor) {
return this.authService.removeAvatar(user.userId);
}
}