Skip to content

Commit

Permalink
refactor: base response 인터셉터 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeonghak committed Nov 25, 2024
1 parent b6546f4 commit 45dbb30
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions apps/server/src/account/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,57 @@
import { Body, Controller, HttpCode, Post, Req, Res, UseGuards } from '@nestjs/common';
import { Body, Controller, Post, Req, Res, UseGuards } from '@nestjs/common';
import { Request, Response } from 'express';
import { AuthService } from '@/account/auth.service';
import { RefreshTokenGuard } from '@/account/guard/refreshToken.guard';
import { AuthUser } from '@/account/decorator/authUser.decorator';
import { Account } from '@/account/entity/account.entity';
import { CreateUserDto } from '@/account/dto/create-user.dto';
import { UserDto } from '@/account/dto/user.dto';
import { BaseResponse } from '@/common/BaseResponse';
import { AuthDto } from '@/account/dto/auth.dto';
import { AccessTokenGuard } from '@/account/guard/accessToken.guard';
import { ResponseMessage } from '@/common/decorator/response-message.decorator';
import { ResponseStatus } from '@/common/decorator/response-status.decorator';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('signup')
@ResponseStatus(201)
@ResponseMessage('회원가입 처리가 완료되었습니다.')
async signUp(@Body() body: CreateUserDto) {
const user = await this.authService.signUp(body.username, body.password);
return new BaseResponse(201, '회원가입 처리가 완료되었습니다.', new UserDto(user));
return new UserDto(user);
}

@Post('signin')
@HttpCode(200)
@ResponseMessage('로그인 처리가 완료되었습니다.')
async signIn(@Body() body: CreateUserDto, @Res({ passthrough: true }) res: Response) {
const signInResult = await this.authService.signIn(body.username, body.password);
res.cookie('refreshToken', signInResult.refreshToken, {
httpOnly: true,
sameSite: 'strict',
});
return new BaseResponse(
200,
'로그인 처리가 완료되었습니다.',
AuthDto.of(signInResult.accessToken, signInResult.user)
);
return AuthDto.of(signInResult.accessToken, signInResult.user);
}

@UseGuards(RefreshTokenGuard)
@Post('refresh')
@HttpCode(200)
@ResponseMessage('토큰 재발급 처리가 완료되었습니다.')
async refresh(@AuthUser() user: Account, @Req() req: Request) {
const { refreshToken } = req.cookies;
const accessToken = await this.authService.refresh(user, refreshToken);
return new BaseResponse(
200,
'토큰 재발급 처리가 완료되었습니다.',
AuthDto.of(accessToken, user)
);
return AuthDto.of(accessToken, user);
}

@UseGuards(AccessTokenGuard)
@Post('signout')
@HttpCode(200)
@ResponseMessage('로그아웃 처리가 완료되었습니다.')
async signOut(@AuthUser() user: Account, @Res({ passthrough: true }) res: Response) {
this.authService.signOut(user);
res.cookie('refreshToken', '', {
httpOnly: true,
sameSite: 'strict',
expires: new Date(0),
});
return new BaseResponse(200, '로그아웃 처리가 완료되었습니다.', {});
}
}

0 comments on commit 45dbb30

Please sign in to comment.