import { Controller, Post, Body, HttpCode } from "@nestjs/common"; import { I18n, I18nContext } from "nestjs-i18n"; import { ApiTags, ApiOperation, ApiOkResponse } from "@nestjs/swagger"; import { AuthService } from "./auth.service"; import { RegisterDto, LoginDto, RefreshTokenDto, TokenResponseDto, } from "./dto/auth.dto"; import { Public } from "../../common/decorators"; import { ApiResponse, createSuccessResponse, } from "../../common/types/api-response.type"; @ApiTags("Auth") @Controller("auth") export class AuthController { constructor(private readonly authService: AuthService) {} @Post("register") @Public() @HttpCode(200) @ApiOperation({ summary: "Register a new user" }) @ApiOkResponse({ description: "User registered successfully", type: TokenResponseDto, }) async register( @Body() dto: RegisterDto, @I18n() i18n: I18nContext, ): Promise> { const result = await this.authService.register(dto); return createSuccessResponse(result, i18n.t("auth.registered"), 201); } @Post("login") @Public() @HttpCode(200) @ApiOperation({ summary: "Login with email and password" }) @ApiOkResponse({ description: "Login successful", type: TokenResponseDto }) async login( @Body() dto: LoginDto, @I18n() i18n: I18nContext, ): Promise> { const result = await this.authService.login(dto); return createSuccessResponse(result, i18n.t("auth.login_success")); } @Post("refresh") @Public() @HttpCode(200) @ApiOperation({ summary: "Refresh access token" }) @ApiOkResponse({ description: "Token refreshed successfully", type: TokenResponseDto, }) async refreshToken( @Body() dto: RefreshTokenDto, @I18n() i18n: I18nContext, ): Promise> { const result = await this.authService.refreshToken(dto.refreshToken); return createSuccessResponse(result, i18n.t("auth.refresh_success")); } @Post("logout") @HttpCode(200) @ApiOperation({ summary: "Logout and invalidate refresh token" }) @ApiOkResponse({ description: "Logout successful", schema: { type: "object", properties: { success: { type: "boolean" }, message: { type: "string" }, data: { type: "null" }, }, }, }) async logout( @Body() dto: RefreshTokenDto, @I18n() i18n: I18nContext, ): Promise> { await this.authService.logout(dto.refreshToken); return createSuccessResponse(null, i18n.t("auth.logout_success")); } }