- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
273 lines
8.3 KiB
TypeScript
273 lines
8.3 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
HttpCode,
|
|
HttpStatus,
|
|
UseGuards,
|
|
Req,
|
|
Res,
|
|
} from "@nestjs/common";
|
|
import type { Response } from "express";
|
|
import { Throttle } from "@nestjs/throttler";
|
|
import { AuthService } from "./auth.service";
|
|
import { SendOtpDto } from "./dto/send-otp.dto";
|
|
import { VerifyOtpDto } from "./dto/verify-otp.dto";
|
|
import { RegisterDto } from "./dto/register.dto";
|
|
import { LoginDto } from "./dto/login.dto";
|
|
import { JwtAuthGuard } from "./jwt-auth.guard";
|
|
import {
|
|
ApiTags,
|
|
ApiOperation,
|
|
ApiResponse,
|
|
ApiOkResponse,
|
|
ApiBadRequestResponse,
|
|
ApiBearerAuth,
|
|
} from "@nestjs/swagger";
|
|
|
|
import { AdminLoginDto } from "./dto/admin-login.dto";
|
|
import { RefreshTokenDto } from "./dto/refresh-token.dto";
|
|
|
|
@ApiTags("Auth - احراز هویت")
|
|
@Controller("auth")
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: "خطای داخلی سرور",
|
|
schema: {
|
|
example: {
|
|
success: false,
|
|
message: "خطای ناشناخته در سرور رخ داده است",
|
|
code: "SERVER_ERROR",
|
|
details: {},
|
|
},
|
|
},
|
|
})
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
|
@Post("send-otp")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: "ارسال کد تایید پیامکی (OTP)" })
|
|
@ApiOkResponse({
|
|
description: "کد با موفقیت به شماره تلفن همراه پیامک شد",
|
|
schema: {
|
|
example: {
|
|
success: true,
|
|
message: "کد تایید با موفقیت به شماره شما پیامک شد.",
|
|
},
|
|
},
|
|
})
|
|
@ApiBadRequestResponse({ description: "شماره تلفن همراه نامعتبر است" })
|
|
sendOtp(@Body() sendOtpDto: SendOtpDto) {
|
|
return this.authService.sendOtp(sendOtpDto);
|
|
}
|
|
|
|
private setAuthCookies(
|
|
res?: Response,
|
|
accessToken?: string,
|
|
refreshToken?: string,
|
|
) {
|
|
if (!res || typeof res.cookie !== "function") return;
|
|
const isProd = process.env.NODE_ENV === "production";
|
|
if (accessToken) {
|
|
res.cookie("accessToken", accessToken, {
|
|
httpOnly: true,
|
|
secure: isProd,
|
|
sameSite: "lax",
|
|
maxAge: 15 * 60 * 1000, // 15 mins
|
|
path: "/",
|
|
});
|
|
}
|
|
if (refreshToken) {
|
|
res.cookie("refreshToken", refreshToken, {
|
|
httpOnly: true,
|
|
secure: isProd,
|
|
sameSite: "lax",
|
|
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
|
|
path: "/",
|
|
});
|
|
}
|
|
}
|
|
|
|
private clearAuthCookies(res?: Response) {
|
|
if (!res || typeof res.clearCookie !== "function") return;
|
|
const isProd = process.env.NODE_ENV === "production";
|
|
res.clearCookie("accessToken", {
|
|
httpOnly: true,
|
|
secure: isProd,
|
|
sameSite: "lax",
|
|
path: "/",
|
|
});
|
|
res.clearCookie("refreshToken", {
|
|
httpOnly: true,
|
|
secure: isProd,
|
|
sameSite: "lax",
|
|
path: "/",
|
|
});
|
|
}
|
|
|
|
@Throttle({ default: { limit: 10, ttl: 60000 } })
|
|
@Post("verify-otp")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: "تایید کد پیامکی و ورود به سیستم" })
|
|
@ApiOkResponse({ description: "کد تایید شد و ورود با موفقیت انجام گردید" })
|
|
@ApiBadRequestResponse({
|
|
description: "کد تایید اشتباه است یا منقضی شده است",
|
|
})
|
|
async verifyOtp(
|
|
@Body() verifyOtpDto: VerifyOtpDto,
|
|
@Res({ passthrough: true }) res?: Response,
|
|
) {
|
|
const result = await this.authService.verifyOtp(verifyOtpDto);
|
|
if (result?.data) {
|
|
this.setAuthCookies(
|
|
res,
|
|
result.data.accessToken,
|
|
result.data.refreshToken,
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
|
@Post("register")
|
|
@HttpCode(HttpStatus.CREATED)
|
|
@ApiOperation({ summary: "ثبتنام با ایمیل/موبایل و رمز عبور" })
|
|
@ApiOkResponse({ description: "کاربر با موفقیت ثبتنام شد" })
|
|
@ApiBadRequestResponse({
|
|
description: "اطلاعات ثبتنام نامعتبر است یا کاربر از قبل وجود دارد",
|
|
})
|
|
async register(
|
|
@Body() registerDto: RegisterDto,
|
|
@Res({ passthrough: true }) res?: Response,
|
|
) {
|
|
const result = await this.authService.register(registerDto);
|
|
if (result?.data) {
|
|
this.setAuthCookies(
|
|
res,
|
|
result.data.accessToken,
|
|
result.data.refreshToken,
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
|
@Post("login")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: "ورود با موبایل و رمز عبور" })
|
|
@ApiOkResponse({ description: "ورود موفق به همراه توکن دسترسی" })
|
|
@ApiBadRequestResponse({ description: "نام کاربری یا رمز عبور اشتباه است" })
|
|
async login(
|
|
@Body() loginDto: LoginDto,
|
|
@Res({ passthrough: true }) res?: Response,
|
|
) {
|
|
const result = await this.authService.login(loginDto);
|
|
if (result?.data) {
|
|
this.setAuthCookies(
|
|
res,
|
|
result.data.accessToken,
|
|
result.data.refreshToken,
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Throttle({ default: { limit: 5, ttl: 300000 } }) // 5 attempts per 5 minutes
|
|
@Post("admin-login")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: "ورود ادمین به پنل مدیریت" })
|
|
@ApiOkResponse({ description: "ورود موفق ادمین به همراه توکن" })
|
|
@ApiBadRequestResponse({ description: "اطلاعات ورود ادمین اشتباه است" })
|
|
async adminLogin(
|
|
@Body() body: AdminLoginDto,
|
|
@Res({ passthrough: true }) res?: Response,
|
|
) {
|
|
const result = await this.authService.adminLogin(body);
|
|
if (result?.data) {
|
|
this.setAuthCookies(
|
|
res,
|
|
result.data.accessToken,
|
|
result.data.refreshToken,
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Throttle({ default: { limit: 10, ttl: 60000 } })
|
|
@Post("refresh-token")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({
|
|
summary: "دریافت جفت توکن جدید با استفاده از توکن تازهسازی",
|
|
})
|
|
@ApiOkResponse({ description: "توکنهای جدید با موفقیت صادر شدند" })
|
|
@ApiBadRequestResponse({ description: "توکن نامعتبر یا منقضی است" })
|
|
async refreshToken(
|
|
@Body() body: RefreshTokenDto,
|
|
@Req() req: any,
|
|
@Res({ passthrough: true }) res?: Response,
|
|
) {
|
|
let token = body?.refreshToken;
|
|
if (!token && req.headers?.cookie) {
|
|
const match = req.headers.cookie.match(/(?:^|;\s*)refreshToken=([^;]+)/);
|
|
if (match) token = decodeURIComponent(match[1]);
|
|
}
|
|
const result = await this.authService.refreshToken(token);
|
|
if (result?.data) {
|
|
this.setAuthCookies(
|
|
res,
|
|
result.data.accessToken,
|
|
result.data.refreshToken,
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post("refresh")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: "تازهسازی توکن دسترسی (Refresh Token)" })
|
|
@ApiOkResponse({ description: "توکن جدید با موفقیت صادر شد" })
|
|
async refresh(
|
|
@Req()
|
|
req: {
|
|
user: { id: string; email?: string; role?: string; mobile?: string };
|
|
},
|
|
@Res({ passthrough: true }) res?: Response,
|
|
) {
|
|
const result = await this.authService.refresh(req.user);
|
|
if (result?.data) {
|
|
this.setAuthCookies(
|
|
res,
|
|
result.data.accessToken,
|
|
result.data.refreshToken,
|
|
);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post("logout")
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: "خروج از حساب کاربری و ابطال توکن" })
|
|
@ApiOkResponse({ description: "با موفقیت خارج شدید" })
|
|
async logout(@Req() req: any, @Res({ passthrough: true }) res?: Response) {
|
|
let token: string | undefined;
|
|
const authHeader = req.headers?.authorization;
|
|
if (authHeader && authHeader.startsWith("Bearer ")) {
|
|
token = authHeader.replace("Bearer ", "").trim();
|
|
} else if (req.headers?.cookie) {
|
|
const match = req.headers.cookie.match(/(?:^|;\s*)accessToken=([^;]+)/);
|
|
if (match) token = decodeURIComponent(match[1]);
|
|
}
|
|
if (token) {
|
|
await this.authService.blacklistToken(token);
|
|
}
|
|
this.clearAuthCookies(res);
|
|
return { success: true, message: "با موفقیت خارج شدید" };
|
|
}
|
|
}
|