training_data/apps/server/src/auth/auth.controller.ts

93 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-01-03 09:24:46 +08:00
import { Controller, Headers, Post, Body, UseGuards, Get, Req, HttpException, HttpStatus, BadRequestException, InternalServerErrorException, NotFoundException, UnauthorizedException, Logger } from '@nestjs/common';
2024-09-09 18:48:07 +08:00
import { AuthService } from './auth.service';
2025-01-06 08:45:23 +08:00
import { AuthSchema, JwtPayload } from '@nice/common';
2024-09-09 18:48:07 +08:00
import { AuthGuard } from './auth.guard';
2024-12-30 08:26:40 +08:00
import { UserProfileService } from './utils';
import { z } from 'zod';
2025-01-03 09:24:46 +08:00
import { FileValidationErrorType } from './types';
2024-09-09 18:48:07 +08:00
@Controller('auth')
export class AuthController {
2025-01-03 09:24:46 +08:00
private logger = new Logger(AuthController.name)
2024-12-30 08:26:40 +08:00
constructor(private readonly authService: AuthService) { }
2025-01-03 09:24:46 +08:00
@Get('file')
async authFileRequset(
@Headers('x-original-uri') originalUri: string,
@Headers('x-real-ip') realIp: string,
@Headers('x-original-method') method: string,
@Headers('x-query-params') queryParams: string,
@Headers('host') host: string,
@Headers('authorization') authorization: string,
) {
2025-01-08 00:52:11 +08:00
2025-01-03 09:24:46 +08:00
try {
const fileRequest = {
originalUri,
realIp,
method,
queryParams,
host,
authorization
};
2025-01-08 00:52:11 +08:00
2025-01-03 09:24:46 +08:00
const authResult = await this.authService.validateFileRequest(fileRequest);
if (!authResult.isValid) {
// 使用枚举类型进行错误处理
switch (authResult.error) {
case FileValidationErrorType.INVALID_URI:
throw new BadRequestException(authResult.error);
case FileValidationErrorType.RESOURCE_NOT_FOUND:
throw new NotFoundException(authResult.error);
case FileValidationErrorType.AUTHORIZATION_REQUIRED:
case FileValidationErrorType.INVALID_TOKEN:
throw new UnauthorizedException(authResult.error);
default:
throw new InternalServerErrorException(authResult.error || FileValidationErrorType.UNKNOWN_ERROR);
}
}
return {
headers: {
'X-User-Id': authResult.userId,
'X-Resource-Type': authResult.resourceType,
},
};
} catch (error: any) {
this.logger.verbose(`File request auth failed from ${realIp} reason:${error.message}`)
throw error;
}
}
2025-01-06 08:45:23 +08:00
@UseGuards(AuthGuard)
2024-12-30 08:26:40 +08:00
@Get('user-profile')
async getUserProfile(@Req() request: Request) {
2025-01-06 08:45:23 +08:00
2024-12-30 08:26:40 +08:00
const payload: JwtPayload = (request as any).user;
const { staff } = await UserProfileService.instance.getUserProfileById(payload.sub);
return staff
}
@Post('login')
async login(@Body() body: z.infer<typeof AuthSchema.signInRequset>) {
return this.authService.signIn(body);
}
@Post('signup')
async signup(@Body() body: z.infer<typeof AuthSchema.signUpRequest>) {
return this.authService.signUp(body);
}
@Post('refresh-token')
async refreshToken(
@Body() body: z.infer<typeof AuthSchema.refreshTokenRequest>,
) {
return this.authService.refreshToken(body);
}
// @UseGuards(AuthGuard)
@Post('logout')
async logout(@Body() body: z.infer<typeof AuthSchema.logoutRequest>) {
return this.authService.logout(body);
}
@UseGuards(AuthGuard) // Protecting the changePassword endpoint with AuthGuard
@Post('change-password')
async changePassword(
@Body() body: z.infer<typeof AuthSchema.changePassword>,
) {
return this.authService.changePassword(body);
}
2024-09-09 18:48:07 +08:00
}