import { Controller, Post, Body, HttpCode, HttpStatus, Req, UseGuards } from '@nestjs/common'; import { Throttle } from '@nestjs/throttler'; import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; import { AuthService } from './auth.service'; import { LoginDto } from './dto/login.dto'; import { RefreshTokenDto } from './dto/refresh-token.dto'; import { ChangePasswordDto } from './dto/change-password.dto'; import { JwtAuthGuard } from './guards/jwt-auth.guard'; @ApiTags('Authentication') @Controller('auth') export class AuthController { constructor(private authService: AuthService) {} @Post('login') @Throttle({ default: { limit: 10, ttl: 60000 } }) @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Login with email and password' }) async login(@Body() dto: LoginDto) { return this.authService.login(dto.email, dto.password); } @Post('refresh') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Refresh access token' }) async refresh(@Body() dto: RefreshTokenDto) { return this.authService.refresh(dto.refreshToken); } @Post('logout') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Logout and revoke refresh token' }) async logout(@Body() dto: RefreshTokenDto) { await this.authService.logout(dto.refreshToken); return { ok: true }; } @Post('change-password') @Throttle({ default: { limit: 10, ttl: 60000 } }) @HttpCode(HttpStatus.OK) @ApiBearerAuth() @UseGuards(JwtAuthGuard) @ApiOperation({ summary: "Change the signed-in user's password; signs out their other sessions and returns new tokens" }) async changePassword(@Body() dto: ChangePasswordDto, @Req() req: any) { return this.authService.changePassword(req.user.id, dto.currentPassword, dto.newPassword); } }