-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauth.controller.ts
More file actions
375 lines (359 loc) · 12.4 KB
/
auth.controller.ts
File metadata and controls
375 lines (359 loc) · 12.4 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Post,
Request,
Res,
UseGuards,
} from "@nestjs/common";
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { LocalAuthGuard } from "./guards/local-auth.guard";
import { AuthService } from "./auth.service";
import { LoginDto } from "./dto/login.dto";
import { SignupDto } from "./dto/signup.dto";
import { ResendEmailDto } from "./dto/resend-email.dto";
import { VerifyEmailDto } from "./dto/verify-email.dto";
import {
BadRequestErrorResponse,
ForbiddenErrorResponse,
LoginUnauthorizedErrorResponse,
NotFoundErrorResponse,
UnauthorizedErrorResponse,
} from "@/global/responses/errors";
import {
LoginResponse,
LogoutResponse,
RefreshResponse,
} from "./auth.response";
import { GenericSuccessResponse } from "../global/responses/shared";
import { ResetPasswordRequestDto } from "./dto/reset-password-request.dto";
import { ResetPasswordDto } from "./dto/reset-password.dto";
import { JwtAuthGuard } from "./guards/jwt-auth.guard";
import { JwtRefreshAuthGuard } from "./guards/jwt-rt-auth.guard";
import { Public } from "@/global/decorators/public.decorator";
import { Unverified } from "@/global/decorators/unverified.decorator";
import { RevokeRTDto } from "./dto/revoke-refresh-token.dto";
import { CheckAbilities } from "@/global/decorators/abilities.decorator";
import { Action } from "@/ability/ability.factory/ability.factory";
import { CustomRequest } from "@/global/types/CustomRequest";
import { Response } from "express";
import { DiscordAuthGuard } from "./guards/discord-auth.guard";
import { AppConfigService } from "@/config/app/appConfig.service";
import { GithubAuthGuard } from "./guards/github-auth.guard";
@ApiTags("Auth")
@Controller("auth")
export class AuthController {
constructor(
private authService: AuthService,
private appConfigService: AppConfigService,
) {}
@ApiOperation({
summary: "Public Route: Signup, and send a verification email",
description:
"[access]: public" +
"<br>Note: Please use a 'real' email if you want to receive a verification email.",
})
@ApiResponse({
status: HttpStatus.OK,
description:
"Signup Success. User created, and verification email sent.",
type: GenericSuccessResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "invalid email, password",
type: BadRequestErrorResponse,
})
@HttpCode(HttpStatus.OK)
@Public()
@Post("signup")
async signup(@Body() signupDto: SignupDto) {
return this.authService.signup(signupDto);
}
@ApiOperation({
summary: "Resend the verification email",
description:
"[access]: unverified user, user, voyage, admin" +
"<br>Please use a 'real' email if you want to receive a verification email." +
"<br>response will always be 200, due to privacy reason",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Email successfully re-sent",
type: GenericSuccessResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Unauthorized",
type: UnauthorizedErrorResponse,
})
@HttpCode(HttpStatus.OK)
@Unverified()
@Post("resend-email")
async resendVerificationEmail(@Body() resendEmailDto: ResendEmailDto) {
return this.authService.resendEmail(resendEmailDto);
}
@ApiOperation({
summary: "Verifies the users email",
description:
"[access]: unverified user, user, voyager, admin" +
"<br>Using a token sent to their email when sign up",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Email verified successfully",
type: GenericSuccessResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description:
"Token error - e.g. malformed token, or expired token. <br> " +
"Specific errors will be returned in <code>res.message</code>",
type: UnauthorizedErrorResponse,
})
@HttpCode(HttpStatus.OK)
@Unverified()
@Post("verify-email")
async verifyEmail(@Body() verifyEmailDto: VerifyEmailDto) {
return this.authService.verifyEmail(verifyEmailDto);
}
@ApiOperation({
summary:
"Public Route: When a user logs in, sets access token and refresh token (http cookies).",
description: "<br>[access]: public",
})
@ApiResponse({
status: HttpStatus.OK,
description:
"User successfully authenticated, jwt token is saved in cookies",
type: LoginResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description:
"Account does not exist. A more generic error message " +
"so users can't tell if the account exist or not due to privacy reason",
type: BadRequestErrorResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Login fails. Usually wrong password",
type: LoginUnauthorizedErrorResponse,
})
@UseGuards(LocalAuthGuard)
@Public()
@HttpCode(HttpStatus.OK)
@Post("login")
async login(
@Body() body: LoginDto,
@Request() req: CustomRequest,
@Res({ passthrough: true }) res: Response,
) {
await this.authService.returnTokensOnLoginSuccess(req, res);
res.status(HttpStatus.OK).send({ message: "Login Success" });
}
@ApiOperation({
summary:
"Bypass access token jwt guard. Refresh an access token, with a valid refresh token in cookies",
description: "<br>[access]: public",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Refresh token is successfully refreshed",
type: RefreshResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "Invalid / tempered access token / no refresh token",
type: UnauthorizedErrorResponse,
})
@ApiResponse({
status: HttpStatus.FORBIDDEN,
description:
"No user found in the database, maybe a tempered jwt token",
type: ForbiddenErrorResponse,
})
@HttpCode(HttpStatus.OK)
@Public()
@UseGuards(JwtRefreshAuthGuard)
@Post("refresh")
async refresh(
@Request() req: CustomRequest,
@Res({ passthrough: true }) res: Response,
) {
const { access_token, refresh_token } = await this.authService.refresh(
req.user,
);
this.authService.setCookie(res, access_token, refresh_token);
res.status(HttpStatus.OK).send({ message: "Refresh Success" });
}
@ApiOperation({
summary:
"[Admin only]: Revokes user's refresh token, with a valid user id or email",
description:
"[access]: admin" +
"<br>using the user's id or email, removes user's refresh token",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Refresh token successfully revoked",
type: GenericSuccessResponse,
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: "User not found.",
type: NotFoundErrorResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "userId and email is provided",
type: BadRequestErrorResponse,
})
@ApiResponse({
status: HttpStatus.FORBIDDEN,
description: "user doesn't have permission to preform operation",
type: ForbiddenErrorResponse,
})
@HttpCode(HttpStatus.OK)
@CheckAbilities({ action: Action.Manage, subject: "all" })
@Delete("refresh/revoke")
async revoke(@Body() body: RevokeRTDto, @Res() res: Response) {
await this.authService.revokeRefreshToken(body);
res.status(HttpStatus.OK).json({
message: "User Refresh token Successfully revoke.",
statusCode: 200,
});
}
@ApiOperation({
summary:
"When a user logs out, access and refresh tokens are cleared from cookies, refresh token is set to null in the database.",
description: "[access]: user, voyager, admin",
})
@ApiResponse({
status: HttpStatus.OK,
description:
"User successfully logs out, access and refresh tokens in cookies is removed.",
type: LogoutResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "Bad request, e.g. missing a required cookie",
type: BadRequestErrorResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: "no access token (i.e. not logged in)",
type: UnauthorizedErrorResponse,
})
@UseGuards(JwtAuthGuard)
@Unverified()
@Post("logout")
async logout(
@Request() req: CustomRequest,
@Res({ passthrough: true }) res: Response,
) {
const cookies = req.cookies;
if (!cookies?.refresh_token)
throw new BadRequestException("No Refresh Token");
await this.authService.logout(res, cookies.refresh_token);
}
@ApiOperation({
summary:
"Public route: Request a password reset - email with password reset link (if the account exists)",
description:
"[access]: unverified user, user, voyager, admin" +
"<br>Please use a 'real' email if you want to receive a password reset email.",
})
@ApiResponse({
status: HttpStatus.OK,
description:
"Password reset email successfully sent (if the user account exist)",
type: GenericSuccessResponse,
})
@Public()
@HttpCode(HttpStatus.OK)
@Post("reset-password/request")
async resetPasswordRequest(
@Body() resetPasswordRequestDto: ResetPasswordRequestDto,
) {
return this.authService.resetPasswordRequest(resetPasswordRequestDto);
}
@ApiOperation({
summary: "Public route: Reset user password",
description:
"[access]: public" +
"<br>The reset token is emailed to them when the request a password reset.",
})
@ApiResponse({
status: HttpStatus.OK,
description: "Password reset email successfully sent",
type: GenericSuccessResponse,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: "Error in request body, e.g. missing or invalid data",
type: BadRequestErrorResponse,
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description:
"Token error - e.g. malformed token, or expired token. <br> " +
"Specific errors will be returned in <code>res.message</code>",
type: UnauthorizedErrorResponse,
})
@HttpCode(HttpStatus.OK)
@Public()
@Post("reset-password/")
async resetPassword(@Body() resetPasswordDto: ResetPasswordDto) {
return this.authService.resetPassword(resetPasswordDto);
}
@ApiOperation({
summary: "discord oauth",
description:
"This does not work on swagger. Open this link from a web browser, a discord popup should appear. `{BaseURL}/api/v1/auth/discord/login` ",
})
@UseGuards(DiscordAuthGuard)
@Public()
@Get("/discord/login")
handleDiscordLogin() {
return;
}
@UseGuards(DiscordAuthGuard)
@Public()
@Get("/discord/redirect")
async handleDiscordRedirect(
@Request() req: CustomRequest,
@Res({ passthrough: true }) res: Response,
) {
await this.authService.returnTokensOnLoginSuccess(req, res);
const FRONTEND_URL = this.appConfigService.FrontendUrl;
res.redirect(`${FRONTEND_URL}`);
}
@ApiOperation({
summary: "Github oauth",
description:
"This does not work on swagger. Open `{BaseURL}/api/v1/auth/github/login` in a browser to see the GitHub popup.",
})
@UseGuards(GithubAuthGuard)
@Public()
@Get("/github/login")
handleGithubLogin() {
return;
}
@UseGuards(GithubAuthGuard)
@Public()
@Get("/github/redirect")
async handleGithubRedirect(
@Request() req: CustomRequest,
@Res({ passthrough: true }) res: Response,
) {
await this.authService.returnTokensOnLoginSuccess(req, res);
const FRONTEND_URL = this.appConfigService.FrontendUrl;
res.redirect(`${FRONTEND_URL}`);
}
}