-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathauth.controller.ts
More file actions
134 lines (122 loc) · 4.94 KB
/
Copy pathauth.controller.ts
File metadata and controls
134 lines (122 loc) · 4.94 KB
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
124
125
126
127
128
129
130
131
132
133
134
import { Controller, Post, Body, HttpCode, HttpStatus, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import {
RegisterDto,
LoginDto,
RefreshTokenDto,
OAuthDto,
ForgotPasswordDto,
ResetPasswordDto,
ChangePasswordDto,
AuthResponseDto,
TokenResponseDto,
} from './dto';
import { Public, CurrentUser, JwtPayload } from '../../common';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
@ApiTags('Auth')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Public()
@Post('register')
@ApiOperation({ summary: 'Register a new user' })
@ApiResponse({ status: 201, description: 'User registered successfully', type: AuthResponseDto })
@ApiResponse({ status: 409, description: 'Email already registered' })
async register(@Body() dto: RegisterDto): Promise<AuthResponseDto> {
return this.authService.register(dto);
}
@Public()
@Post('login')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Login with email and password' })
@ApiResponse({ status: 200, description: 'Login successful', type: AuthResponseDto })
@ApiResponse({ status: 401, description: 'Invalid credentials' })
async login(@Body() dto: LoginDto): Promise<AuthResponseDto> {
return this.authService.login(dto);
}
@Public()
@Post('refresh')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Refresh access token' })
@ApiResponse({ status: 200, description: 'Token refreshed', type: TokenResponseDto })
@ApiResponse({ status: 401, description: 'Invalid refresh token' })
async refresh(@Body() dto: RefreshTokenDto): Promise<TokenResponseDto> {
return this.authService.refreshToken(dto);
}
@Post('logout')
@HttpCode(HttpStatus.NO_CONTENT)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Logout and invalidate refresh token' })
@ApiResponse({ status: 204, description: 'Logged out successfully' })
async logout(@Body() dto: RefreshTokenDto): Promise<void> {
await this.authService.logout(dto.refreshToken);
}
@Public()
@Post('google')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Authenticate with Google' })
@ApiResponse({ status: 200, description: 'Google auth successful', type: AuthResponseDto })
@ApiResponse({ status: 401, description: 'Invalid Google token' })
async googleAuth(@Body() dto: OAuthDto): Promise<AuthResponseDto> {
return this.authService.googleAuth(dto);
}
@Public()
@Post('apple')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Authenticate with Apple' })
@ApiResponse({ status: 200, description: 'Apple auth successful', type: AuthResponseDto })
@ApiResponse({ status: 401, description: 'Invalid Apple token' })
async appleAuth(@Body() dto: OAuthDto): Promise<AuthResponseDto> {
return this.authService.appleAuth(dto);
}
@Public()
@Post('forgot-password')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({ summary: 'Request password reset email' })
@ApiResponse({ status: 204, description: 'Reset email sent if account exists' })
async forgotPassword(@Body() dto: ForgotPasswordDto): Promise<void> {
await this.authService.forgotPassword(dto.email);
}
@Public()
@Post('reset-password')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({ summary: 'Reset password with token' })
@ApiResponse({ status: 204, description: 'Password reset successful' })
@ApiResponse({ status: 400, description: 'Invalid or expired token' })
async resetPassword(@Body() dto: ResetPasswordDto): Promise<void> {
await this.authService.resetPassword(dto.token, dto.password);
}
@Post('change-password')
@HttpCode(HttpStatus.NO_CONTENT)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Change password for authenticated user' })
@ApiResponse({ status: 204, description: 'Password changed successfully' })
@ApiResponse({ status: 401, description: 'Invalid current password' })
async changePassword(
@CurrentUser() user: JwtPayload,
@Body() dto: ChangePasswordDto,
): Promise<void> {
await this.authService.changePassword(user.sub, dto.oldPassword, dto.newPassword);
}
@Public()
@Post('verify-email')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({ summary: 'Verify email with token' })
@ApiResponse({ status: 204, description: 'Email verified successfully' })
@ApiResponse({ status: 400, description: 'Invalid or expired token' })
async verifyEmail(@Body('token') token: string): Promise<void> {
await this.authService.verifyEmail(token);
}
@Post('resend-verification')
@HttpCode(HttpStatus.NO_CONTENT)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Resend verification email' })
@ApiResponse({ status: 204, description: 'Verification email sent' })
async resendVerification(@CurrentUser() user: JwtPayload): Promise<void> {
await this.authService.resendVerificationEmail(user.sub);
}
}