Files
fiberops-api/src/auth/auth.controller.ts
2026-04-13 09:36:55 +08:00

63 lines
1.8 KiB
TypeScript

import {
Controller,
Post,
Get,
Body,
UseGuards,
Req,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Throttle, SkipThrottle } from '@nestjs/throttler';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { RegisterTenantDto } from './dto/register-tenant.dto';
import { RefreshTokenDto } from './dto/refresh-token.dto';
import { ChangePasswordDto } from './dto/change-password.dto';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('login')
@HttpCode(HttpStatus.OK)
@Throttle({ short: { ttl: 60000, limit: 50 } }) // 50 login attempts per minute per IP
async login(@Body() dto: LoginDto) {
return this.authService.login(dto);
}
@Post('register')
@Throttle({ short: { ttl: 60000, limit: 3 } }) // 3 registrations per minute
async register(@Body() dto: RegisterTenantDto) {
return this.authService.registerTenant(dto);
}
@Post('refresh')
@HttpCode(HttpStatus.OK)
async refresh(@Body() dto: RefreshTokenDto) {
return this.authService.refreshTokens(dto.refreshToken);
}
@Post('logout')
@UseGuards(AuthGuard('jwt'))
@HttpCode(HttpStatus.OK)
async logout(@Req() req: any) {
await this.authService.logout(req.user.sub);
return { message: 'Logged out successfully' };
}
@Get('profile')
@UseGuards(AuthGuard('jwt'))
async profile(@Req() req: any) {
return this.authService.getProfile(req.user.sub);
}
@Post('change-password')
@UseGuards(AuthGuard('jwt'))
@HttpCode(HttpStatus.OK)
async changePassword(@Req() req: any, @Body() dto: ChangePasswordDto) {
return this.authService.changePassword(req.user.sub, dto.currentPassword, dto.newPassword);
}
}