From f12ea52f0aae5ca8ac165c6c7e994b832a056560 Mon Sep 17 00:00:00 2001 From: daisy-kim Date: Tue, 13 Aug 2024 23:39:02 +0900 Subject: [PATCH] Refactor: Add error case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 닉네임이 설정되지 않은 유저일 때, 에러케이스 추가 --- src/auth/auth.controller.ts | 14 ++++++-------- src/auth/strategies/jwt.strategy.ts | 11 +++++++++-- src/common/helper/cookie.helper.ts | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 src/common/helper/cookie.helper.ts diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index a9371dc..269ca72 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -6,6 +6,7 @@ import { Response } from 'express'; import { KakaoInfo } from 'src/common/decorators/kakao-info.decorator'; import { KakaoGuard } from 'src/common/guards/kakao.guard'; +import { getCookieOption } from 'src/common/helper/cookie.helper'; import { UserProvider } from 'src/entities'; import { AuthService } from './auth.service'; @@ -32,14 +33,11 @@ export class AuthController { kakaoRefreshToken: refreshToken, }); - res.cookie('Authorization', 'Bearer ' + user.accessToken, { - httpOnly: true, - sameSite: 'none', - secure: true, - path: '/', - expires: new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 1 * 365), - domain: '.korrk.kr', - }); + res.cookie( + 'Authorization', + 'Bearer ' + user.accessToken, + getCookieOption(), + ); return res.redirect(302, this.configService.get('CLIENT_URL')); } diff --git a/src/auth/strategies/jwt.strategy.ts b/src/auth/strategies/jwt.strategy.ts index d7f7f93..12e4b83 100644 --- a/src/auth/strategies/jwt.strategy.ts +++ b/src/auth/strategies/jwt.strategy.ts @@ -1,9 +1,14 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { + Injectable, + NotFoundException, + UnauthorizedException, +} from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy, VerifiedCallback } from 'passport-jwt'; +import { User } from 'src/entities/index'; import { UserService } from 'src/user/user.service'; @Injectable() @@ -19,12 +24,14 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { } async validate(payload: { id: number }, done: VerifiedCallback) { - const user = await this.userService.findOne({ + const user: User = await this.userService.findOne({ id: payload.id, }); if (!user) { throw new UnauthorizedException('존재하지 않는 유저입니다.'); } + if (user.nickname == null) + throw new NotFoundException('닉네임 설정을 완료해주세요.'); done(null, user); } } diff --git a/src/common/helper/cookie.helper.ts b/src/common/helper/cookie.helper.ts new file mode 100644 index 0000000..73eccdc --- /dev/null +++ b/src/common/helper/cookie.helper.ts @@ -0,0 +1,21 @@ +import { CookieOptions } from 'express'; + +import { getNodeEnv } from 'src/common/helper/env.helper'; +import { NODE_ENVIRONMENT } from 'src/common/helper/env.validation'; + +export const getCookieOption = (): CookieOptions => { + if (getNodeEnv === NODE_ENVIRONMENT.development) { + return { + path: '/', + maxAge: 360000, + }; + } + return { + httpOnly: true, + sameSite: 'none', + secure: true, + path: '/', + expires: new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 1 * 365), + domain: '.korrk.kr', + }; +};