All files / auth auth.controller.ts

100% Statements 29/29
100% Branches 0/0
100% Functions 8/8
100% Lines 28/28

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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 1982x                         2x                             2x   2x 2x 2x 2x 2x 2x 2x 2x       2x           2x 2x                             2x 8x                                 2x 5x                             2x     2x                                           2x 4x                                           2x 5x                             2x 5x                                                       2x       2x      
import {
  Body,
  ClassSerializerInterceptor,
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Post,
  Req,
  SerializeOptions,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common'
import {
  ApiBadRequestResponse,
  ApiBearerAuth,
  ApiBody,
  ApiConflictResponse,
  ApiCreatedResponse,
  ApiInternalServerErrorResponse,
  ApiNotFoundResponse,
  ApiOkResponse,
  ApiOperation,
  ApiQuery,
  ApiServiceUnavailableResponse,
  ApiTags,
  ApiUnauthorizedResponse,
} from '@nestjs/swagger'
import { JwtGuard } from '../shared/guards/jwt.guard'
import { RequestWithJWTPayload } from '../shared/interfaces/request-with-user.interface'
import { AuthService } from './auth.service'
import { ForgotPasswordDTO } from './dtos/forgot-password.dto'
import { LoginDTO } from './dtos/login.dto'
import { RegisterDTO } from './dtos/register.dto'
import { SubmitNewPasswordDTO } from './dtos/submit-new-password.dto'
import { PasswordResetTokenGuard } from './guards/pw-reset-token.guard'
import { RefreshTokenGuard } from './guards/refresh-token.guard'
import { VerifyTokenGuard } from './guards/verify-token.guard'
import { RequestWithPasswordResetPayload } from './interfaces/request-with-pw-reset-jwt-payload.interface'
import { RequestWithDbUser } from './interfaces/request-with-refresh-payload.interface'
import { RequestWithVerifyContent } from './interfaces/request-with-verify-payload.interface'
import { TokenResponse } from './responses/token.response'
 
@Controller('auth')
@ApiTags('auth')
@UseInterceptors(ClassSerializerInterceptor)
@SerializeOptions({ strategy: 'excludeAll' })
export class AuthController {
  constructor(private readonly authService: AuthService) {}
 
  @Post('register')
  @ApiBody({ type: RegisterDTO })
  @ApiOperation({ summary: 'Register new user' })
  @ApiConflictResponse({
    description: 'User properties already in use',
  })
  @ApiBadRequestResponse({
    description: 'Provided body did not comply to specs of DTO',
  })
  @ApiCreatedResponse({
    description: 'User has been created',
    type: TokenResponse,
  })
  async register(@Body() registerData: RegisterDTO): Promise<TokenResponse> {
    return new TokenResponse(await this.authService.register(registerData))
  }
 
  @Post('login')
  @HttpCode(HttpStatus.OK)
  @ApiBody({ type: LoginDTO })
  @ApiOperation({ summary: 'Login existing user' })
  @ApiUnauthorizedResponse({
    description: 'User credentials are invalid/User has been banned',
  })
  @ApiOkResponse({
    description: 'Authorized',
    type: TokenResponse,
  })
  @ApiBadRequestResponse({
    description: 'Provided body did not comply to specs of DTO',
  })
  async login(@Body() loginData: LoginDTO): Promise<TokenResponse> {
    return new TokenResponse(await this.authService.login(loginData))
  }
 
  @Post('refresh-token')
  @HttpCode(HttpStatus.OK)
  @UseGuards(RefreshTokenGuard)
  @ApiOperation({ summary: 'Fetch new access token using refresh token' })
  @ApiBearerAuth('refresh_token')
  @ApiOkResponse({
    description: 'Valid refresh token.',
    type: TokenResponse,
  })
  @ApiUnauthorizedResponse({
    description: 'Invalid or expired JWT',
  })
  async getAuthViaRefreshToken(
    @Req() { user }: RequestWithDbUser,
  ): Promise<TokenResponse> {
    return new TokenResponse(await this.authService.getAuthPayload(user))
  }
 
  @Get('verify-email')
  @UseGuards(VerifyTokenGuard)
  @ApiOperation({ summary: 'Verify a users email' })
  @ApiOkResponse({
    description: 'Email has been verified',
  })
  @ApiNotFoundResponse({
    description: 'User with that email has not been found',
  })
  @ApiConflictResponse({
    description: 'User`s email has already been verified',
  })
  @ApiUnauthorizedResponse({
    description: 'Invalid or expired JWT',
  })
  @ApiQuery({
    name: 'token',
    description: 'The verify token as provided by the link in the reset email',
  })
  async verifyMail(@Req() { user }: RequestWithVerifyContent) {
    await this.authService.verifyUserMail(user.email)
  }
 
  @Get('request-verify-email')
  @UseGuards(JwtGuard)
  @ApiOperation({
    summary: 'Request a verfication email for the users email address',
  })
  @ApiBearerAuth('access_token')
  @ApiOkResponse({
    description: 'Verify email has been send',
  })
  @ApiServiceUnavailableResponse({
    description: 'Email server could not be reached',
  })
  @ApiNotFoundResponse({
    description: 'Specified user could not be found (this SHOULD never happen)',
  })
  @ApiUnauthorizedResponse({
    description: 'Invalid or expired JWT',
  })
  @ApiBearerAuth('access_token')
  async requestVerifyMail(@Req() { user }: RequestWithJWTPayload) {
    await this.authService.requestUserVerifyMail(user.id)
  }
 
  @Post('forgot-password')
  @ApiOperation({ summary: 'Request a password reset email' })
  @ApiCreatedResponse({ description: 'Password reset email has been sent' })
  @ApiBody({
    type: ForgotPasswordDTO,
  })
  @ApiServiceUnavailableResponse({
    description: 'Mail could not be sent',
  })
  @ApiBadRequestResponse({
    description: 'Provided body did not comply to specs of DTO',
  })
  async startForgottenPasswordFlow(@Body() { email }: ForgotPasswordDTO) {
    await this.authService.startForgottenPasswordFlow(email)
  }
 
  @Post('forgot-password-submit')
  @UseGuards(PasswordResetTokenGuard)
  @ApiOperation({
    summary: 'Submit new password for user',
  })
  @ApiUnauthorizedResponse({
    description: 'Invalid or expired JWT',
  })
  @ApiCreatedResponse({
    description: 'Password has been updated',
  })
  @ApiServiceUnavailableResponse({
    description: 'Password could not be updated',
  })
  @ApiInternalServerErrorResponse({
    description:
      'The user was deleted or cannot be found anymore...something is off',
  })
  @ApiBadRequestResponse({
    description: 'Provided body did not comply to specs of DTO',
  })
  @ApiQuery({
    name: 'token',
    description: 'The reset token as provided by the link in the reset email',
  })
  async setNewPassword(
    @Req() { user }: RequestWithPasswordResetPayload,
    @Body() { password }: SubmitNewPasswordDTO,
  ) {
    await this.authService.setNewUserPassword(user.id, password)
  }
}