Spaces:
Running
Running
File size: 3,722 Bytes
639bb77 | 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 | 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);
}
}
|