# Code coverage report for modules/auth/auth.controller.ts

# All files / modules/auth auth.controller.ts

88.63% Statements 39/44
0% Branches 0/5
100% Functions 11/11
88.09% Lines 37/42

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
    198
    199
    200
    201
    202
    203
    204
    2051x

    1x

    1x
    1x

    1x
    1x
    1x
    1x

    1x
    10x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

     import {
      Controller,
      Post,
      Body,
      Get,
      UseGuards,
      Req,
      HttpCode,
      HttpStatus,
      BadRequestException,
    } from '@nestjs/common';
    import { Request } from 'express';
    import {
      ApiTags,
      ApiOperation,
      ApiResponse,
      ApiBearerAuth,
      ApiHeader,
    } from '@nestjs/swagger';
    import { AuthService, AuthResponse } from './services/auth.service';
    import {
      LoginDto,
      RegisterDto,
      RequestPasswordResetDto,
      ResetPasswordDto,
      ChangePasswordDto,
    } from './dto';
    import { JwtAuthGuard } from './guards/jwt-auth.guard';
    import { Public } from './decorators/public.decorator';
    import { CurrentUser } from './decorators/current-user.decorator';
    import { CurrentTenant } from './decorators/tenant.decorator';
    import { RequestUser } from './strategies/jwt.strategy';

    @ApiTags('auth')
    @Controller('auth')
    export class AuthController {
      constructor(private readonly authService: AuthService) {}

      @Post('register')
      @Public()
      @ApiOperation({ summary: 'Register new user' })
      @ApiHeader({ name: 'x-tenant-id', required: true, description: 'Tenant ID' })
      @ApiResponse({ status: 201, description: 'User registered successfully' })
      @ApiResponse({ status: 400, description: 'Bad request' })
      @ApiResponse({ status: 409, description: 'Email already exists' })
      async register(
        @Body() dto: RegisterDto,
        @CurrentTenant() tenantId: string,
        @Req() req: Request,
      ): Promise<AuthResponse> {
        Iif (!tenantId) {
          throw new BadRequestException('Tenant ID es requerido');
        }

        return this.authService.register(
          dto,
          tenantId,
          req.ip,
          req.headers['user-agent'],
        );
      }

      @Post('login')
      @Public()
      @HttpCode(HttpStatus.OK)
      @ApiOperation({ summary: 'Login user' })
      @ApiHeader({ name: 'x-tenant-id', required: true, description: 'Tenant ID' })
      @ApiResponse({ status: 200, description: 'Login successful' })
      @ApiResponse({ status: 401, description: 'Invalid credentials' })
      async login(
        @Body() dto: LoginDto,
        @CurrentTenant() tenantId: string,
        @Req() req: Request,
      ): Promise<AuthResponse> {
        Iif (!tenantId) {
          throw new BadRequestException('Tenant ID es requerido');
        }

        return this.authService.login(
          dto,
          tenantId,
          req.ip,
          req.headers['user-agent'],
        );
      }

      @Post('logout')
      @UseGuards(JwtAuthGuard)
      @HttpCode(HttpStatus.OK)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Logout user' })
      @ApiResponse({ status: 200, description: 'Logout successful' })
      async logout(
        @CurrentUser() user: RequestUser,
        @Body('sessionToken') sessionToken: string,
      ): Promise<{ message: string }> {
        await this.authService.logout(user.id, sessionToken);
        return { message: 'Sesión cerrada correctamente' };
      }

      @Post('logout-all')
      @UseGuards(JwtAuthGuard)
      @HttpCode(HttpStatus.OK)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Logout all sessions' })
      @ApiResponse({ status: 200, description: 'All sessions closed' })
      async logoutAll(@CurrentUser() user: RequestUser): Promise<{ message: string }> {
        await this.authService.logoutAll(user.id);
        return { message: 'Todas las sesiones cerradas' };
      }

      @Post('refresh')
      @Public()
      @HttpCode(HttpStatus.OK)
      @ApiOperation({ summary: 'Refresh access token' })
      @ApiResponse({ status: 200, description: 'Token refreshed' })
      @ApiResponse({ status: 401, description: 'Invalid refresh token' })
      async refresh(
        @Body('refreshToken') refreshToken: string,
        @Req() req: Request,
      ): Promise<{ accessToken: string; refreshToken: string }> {
        return this.authService.refreshToken(
          refreshToken,
          req.ip,
          req.headers['user-agent'],
        );
      }

      @Post('password/request-reset')
      @Public()
      @HttpCode(HttpStatus.OK)
      @ApiOperation({ summary: 'Request password reset' })
      @ApiHeader({ name: 'x-tenant-id', required: true, description: 'Tenant ID' })
      @ApiResponse({ status: 200, description: 'Reset email sent if user exists' })
      async requestPasswordReset(
        @Body() dto: RequestPasswordResetDto,
        @CurrentTenant() tenantId: string,
      ): Promise<{ message: string }> {
        Iif (!tenantId) {
          throw new BadRequestException('Tenant ID es requerido');
        }

        return this.authService.requestPasswordReset(dto.email, tenantId);
      }

      @Post('password/reset')
      @Public()
      @HttpCode(HttpStatus.OK)
      @ApiOperation({ summary: 'Reset password with token' })
      @ApiHeader({ name: 'x-tenant-id', required: true, description: 'Tenant ID' })
      @ApiResponse({ status: 200, description: 'Password reset successful' })
      @ApiResponse({ status: 400, description: 'Invalid or expired token' })
      async resetPassword(
        @Body() dto: ResetPasswordDto,
        @CurrentTenant() tenantId: string,
      ): Promise<{ message: string }> {
        Iif (!tenantId) {
          throw new BadRequestException('Tenant ID es requerido');
        }

        return this.authService.resetPassword(dto.token, dto.password, tenantId);
      }

      @Post('password/change')
      @UseGuards(JwtAuthGuard)
      @HttpCode(HttpStatus.OK)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Change password' })
      @ApiResponse({ status: 200, description: 'Password changed' })
      @ApiResponse({ status: 400, description: 'Invalid current password' })
      async changePassword(
        @CurrentUser() user: RequestUser,
        @Body() dto: ChangePasswordDto,
      ): Promise<{ message: string }> {
        return this.authService.changePassword(user.id, dto);
      }

      @Post('verify-email')
      @Public()
      @HttpCode(HttpStatus.OK)
      @ApiOperation({ summary: 'Verify email with token' })
      @ApiHeader({ name: 'x-tenant-id', required: true, description: 'Tenant ID' })
      @ApiResponse({ status: 200, description: 'Email verified' })
      @ApiResponse({ status: 400, description: 'Invalid or expired token' })
      async verifyEmail(
        @Body('token') token: string,
        @CurrentTenant() tenantId: string,
      ): Promise<{ message: string }> {
        Iif (!tenantId) {
          throw new BadRequestException('Tenant ID es requerido');
        }

        return this.authService.verifyEmail(token, tenantId);
      }

      @Get('me')
      @UseGuards(JwtAuthGuard)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Get current user profile' })
      @ApiResponse({ status: 200, description: 'Current user profile' })
      async getProfile(@CurrentUser() user: RequestUser) {
        return this.authService.getProfile(user.id);
      }
    }