Files
iddaai-be/src/modules/auth/auth.controller.ts
T
fahricansecer a338d02244
Deploy Iddaai Backend / build-and-deploy (push) Successful in 2m42s
main
2026-04-26 03:07:18 +03:00

89 lines
2.5 KiB
TypeScript
Executable File

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<ApiResponse<TokenResponseDto>> {
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<ApiResponse<TokenResponseDto>> {
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<ApiResponse<TokenResponseDto>> {
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<ApiResponse<null>> {
await this.authService.logout(dto.refreshToken);
return createSuccessResponse(null, i18n.t("auth.logout_success"));
}
}