import { Controller, Post, Get, Patch, Body, HttpCode, HttpStatus, UseGuards, Ip, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, } from '@nestjs/swagger'; import { AuthService } from './auth.service'; import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; import { Public } from '../../common/decorators/public.decorator'; import { CurrentUser } from '../../common/decorators/current-user.decorator'; import { LoginDto, RegisterDto, RefreshTokenDto, UpdateProfileDto, ChangePasswordDto, } from './dto'; @ApiTags('Auth') @Controller('auth') export class AuthController { constructor(private authService: AuthService) {} @Public() @Post('register') @HttpCode(HttpStatus.CREATED) @ApiOperation({ summary: 'Register new account' }) @ApiResponse({ status: 201, description: 'User registered successfully' }) @ApiResponse({ status: 409, description: 'Email already exists' }) async register(@Body() dto: RegisterDto): Promise { return this.authService.register(dto); } @Public() @Post('login') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Login with email and password' }) @ApiResponse({ status: 200, description: 'Login successful' }) @ApiResponse({ status: 401, description: 'Invalid credentials' }) async login(@Body() dto: LoginDto, @Ip() ip: string): Promise { return this.authService.login(dto, ip); } @Post('logout') @HttpCode(HttpStatus.OK) @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Logout current user' }) @ApiResponse({ status: 200, description: 'Logout successful' }) async logout( @CurrentUser('userId') userId: string, @CurrentUser('token') token: string, ): Promise { return this.authService.logout(userId, token); } @Public() @Post('refresh') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Refresh access token' }) @ApiResponse({ status: 200, description: 'Token refreshed successfully' }) @ApiResponse({ status: 401, description: 'Invalid refresh token' }) async refreshToken(@Body() dto: RefreshTokenDto): Promise { return this.authService.refreshToken(dto); } @Get('profile') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get current user profile' }) @ApiResponse({ status: 200, description: 'Profile retrieved successfully' }) async getProfile(@CurrentUser('userId') userId: string): Promise { return this.authService.getProfile(userId); } @Patch('profile') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Update user profile' }) @ApiResponse({ status: 200, description: 'Profile updated successfully' }) async updateProfile( @CurrentUser('userId') userId: string, @Body() dto: UpdateProfileDto, ): Promise { return this.authService.updateProfile(userId, dto); } @Post('change-password') @HttpCode(HttpStatus.OK) @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Change password' }) @ApiResponse({ status: 200, description: 'Password changed successfully' }) @ApiResponse({ status: 400, description: 'Invalid current password' }) async changePassword( @CurrentUser('userId') userId: string, @Body() dto: ChangePasswordDto, ): Promise { return this.authService.changePassword(userId, dto); } @Public() @Post('forgot-password') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Request password reset' }) @ApiResponse({ status: 200, description: 'Password reset email sent' }) async forgotPassword(@Body('email') email: string): Promise { return this.authService.forgotPassword(email); } }