import { Body, Controller, Get, Logger, Post, Query, Req, Res, UseGuards, UseInterceptors, UploadedFile } from '@nestjs/common';
import { Response } from 'express';
import { AuthService } from './auth.service';
import { SignUpRequestDto } from './dto/sign-up-request.dto';
import { User } from "src/user/user.entity";
import { SignInRequestDto } from './dto/sign-in-request.dto';
import { AuthGuard } from '@nestjs/passport';
import { GetUser } from './get-user.decorator';
import { UserResponseDto } from '../user/dto/user-response.dto';
import { ApiResponse } from 'src/common/api-response.dto';
import { ProfileService } from 'src/file/profile-file.service';
import { FileInterceptor } from '@nestjs/platform-express';
@Controller('api/auth')
export class AuthController {
private readonly logger = new Logger(AuthController.name);
constructor(private authService: AuthService, private profileService: ProfileService){}
@Post('/signup')
@UseInterceptors(FileInterceptor('profilePicture'))
async signUp(@Body() signUpRequestDto: SignUpRequestDto, @UploadedFile() file: Express.Multer.File): Promise<ApiResponse<UserResponseDto>> {
this.logger.verbose(`Attempting to sign up user with email: ${signUpRequestDto.email}`);
const user = await this.authService.signUp(signUpRequestDto);
if (file) {
await this.profileService.uploadProfilePicture(file, user.id);
}
const userResponseDto = new UserResponseDto(user);
this.logger.verbose(`User signed up successfully: ${JSON.stringify(userResponseDto)}`);
return new ApiResponse(true, 201, 'User signed up successfully', userResponseDto);
}
@Post('/signin')
async signIn(@Body() signInRequestDto: SignInRequestDto, @Res() res: Response): Promise<void> {
this.logger.verbose(`Attempting to sign in user with email: ${signInRequestDto.email}`);
const { jwtToken, user } = await this.authService.signIn(signInRequestDto);
const userResponseDto = new UserResponseDto(user);
this.logger.verbose(`User signed in successfully: ${JSON.stringify(userResponseDto)}`);
res.cookie('Authorization', jwtToken, {
httpOnly: false,
secure: false,
maxAge: 3600000,
sameSite: 'lax',
});
res.status(200).json(new ApiResponse(true, 200, 'Sign in successful', { jwtToken, user: userResponseDto }));
}
@Post('/test')
@UseGuards(AuthGuard())
async testForAuth(@GetUser() user: User): Promise<ApiResponse<UserResponseDto>> {
this.logger.verbose(`Authenticated user accessing test route: ${user.email}`);
const userResponseDto = new UserResponseDto(user);
return new ApiResponse(true, 200, 'You are authenticated', userResponseDto);
}
@Get('/kakao')
@UseGuards(AuthGuard('kakao'))
async kakaoLogin(@Req() req: Request) {
}
@Get('kakao/callback')
async kakaoCallback(@Query('code') kakaoAuthResCode: string, @Res() res: Response) {
const { jwtToken, user } = await this.authService.signInWithKakao(kakaoAuthResCode);
res.cookie('Authorization', jwtToken, {
httpOnly: true,
secure: false,
maxAge: 3600000,
sameSite: 'lax',
});
const userResponseDto = new UserResponseDto(user);
this.logger.verbose(`User signed in successfully: ${JSON.stringify(userResponseDto)}`);
res.status(200).json(new ApiResponse(true, 200, 'Sign in successful', { jwtToken, user: userResponseDto }));
}
}