All files / src/auth auth.controller.ts

100% Statements 23/23
100% Branches 0/0
100% Functions 5/5
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1561x                 1x                 1x 1x 1x 1x 1x 1x 1x   1x 1x       1x 10x                                                                             1x     2x                                             1x       3x                                           1x     2x                                                 1x 3x      
import {
  Controller,
  Post,
  Body,
  UseGuards,
  ValidationPipe,
  HttpCode,
  HttpStatus,
} from "@nestjs/common";
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiBadRequestResponse,
  ApiUnauthorizedResponse,
  ApiConflictResponse,
} from "@nestjs/swagger";
import { AuthService } from "./auth.service";
import { LocalAuthGuard } from "./guards/local-auth.guard";
import { Public } from "./decorators/public.decorator";
import { CurrentUser } from "./decorators/current-user.decorator";
import { RegisterDto } from "./dto/register.dto";
import { LoginDto } from "./dto/login.dto";
import { User } from "../users/user.entity";
import { AuthResponse } from "@calendar-todo/shared-types";
import { AuthResponseDto } from "./dto/auth-response.dto";
import { RefreshTokenRequestDto } from "./dto/refresh-token-request.dto";
 
@ApiTags("auth")
@Controller("auth")
export class AuthController {
  constructor(private readonly authService: AuthService) {}
 
  @Public()
  @Post("register")
  @ApiOperation({ summary: "Register a new user" })
  @ApiResponse({
    status: 201,
    description: "User successfully registered",
    type: AuthResponseDto,
  })
  @ApiBadRequestResponse({
    description: "Invalid input data or password requirements not met",
    schema: {
      properties: {
        statusCode: { type: "number", example: 400 },
        message: {
          oneOf: [
            { type: "string", example: "Validation failed" },
            {
              type: "array",
              items: { type: "string" },
              example: ["Email must be a valid email", "Password is too weak"],
            },
          ],
        },
        error: { type: "string", example: "Bad Request" },
      },
    },
  })
  @ApiConflictResponse({
    description: "Email or username already exists",
    schema: {
      properties: {
        statusCode: { type: "number", example: 409 },
        message: { type: "string", example: "이미 가입된 이메일입니다" },
        error: { type: "string", example: "Conflict" },
      },
    },
  })
  async register(
    @Body(ValidationPipe) registerDto: RegisterDto,
  ): Promise<AuthResponse> {
    return this.authService.register(registerDto);
  }
 
  @Public()
  @UseGuards(LocalAuthGuard)
  @Post("login")
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: "Login user" })
  @ApiResponse({
    status: 200,
    description: "User successfully logged in",
    type: AuthResponseDto,
  })
  @ApiUnauthorizedResponse({
    description: "Invalid credentials",
    schema: {
      properties: {
        statusCode: { type: "number", example: 401 },
        message: { type: "string", example: "Invalid credentials" },
        error: { type: "string", example: "Unauthorized" },
      },
    },
  })
  async login(
    @Body(ValidationPipe) loginDto: LoginDto,
    @CurrentUser() user: User,
  ): Promise<AuthResponse> {
    return this.authService.login(user, loginDto.rememberMe);
  }
 
  @Public()
  @Post("refresh")
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: "Refresh access token" })
  @ApiResponse({
    status: 200,
    description: "Token successfully refreshed",
    type: AuthResponseDto,
  })
  @ApiBadRequestResponse({
    description: "Invalid refresh token",
    schema: {
      properties: {
        statusCode: { type: "number", example: 400 },
        message: { type: "string", example: "Invalid refresh token" },
        error: { type: "string", example: "Bad Request" },
      },
    },
  })
  async refresh(
    @Body() refreshTokenRequest: RefreshTokenRequestDto,
  ): Promise<AuthResponse> {
    return this.authService.refreshToken(refreshTokenRequest.refreshToken);
  }
 
  @ApiBearerAuth("JWT-auth")
  @Post("logout")
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: "Logout user" })
  @ApiResponse({
    status: 200,
    description: "User successfully logged out",
    schema: {
      properties: {
        message: { type: "string", example: "Logged out successfully" },
      },
    },
  })
  @ApiUnauthorizedResponse({
    description: "Invalid or missing JWT token",
    schema: {
      properties: {
        statusCode: { type: "number", example: 401 },
        message: { type: "string", example: "Unauthorized" },
      },
    },
  })
  logout(): { message: string } {
    return { message: "Logged out successfully" };
  }
}