092_UI_core / src /modules /auth /auth.controller.ts
anotherath's picture
fix ui and core
639bb77
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
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<any> {
return this.authService.forgotPassword(email);
}
}