From 25f9274d38ea8713fec7c7c565da0ac7ebd5c774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=9A=B0?= Date: Fri, 6 Dec 2024 18:13:40 +0900 Subject: [PATCH] =?UTF-8?q?feat/#39/jwt=20=EC=BF=A0=ED=82=A4=20access?= =?UTF-8?q?=EC=99=80=20refresh=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth/auth.controller.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 2aaae33..d55c345 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -21,18 +21,43 @@ export class AuthController { @Get('google/callback') @UseGuards(AuthGuard('google')) async googleLogin(@User() user: any, @Res() res: Response) { - const jwtToken = await this.authService.googleLogin(user); - console.log(jwtToken); + const { accessToken, refreshToken } = + await this.authService.googleLogin(user); // 쿠키와 리다이렉트의 설정은 클라이언트가 원하는 곳으로 지정해준다. // res.cookie 의 domain부분과 res.redirect의 url부분의 도메인을 일치시켜야 한다. - res.cookie('jwt', jwtToken, { + const cookieOptions = { httpOnly: true, secure: true, domain: this.configService.get('COOKIE_DOMAIN'), - sameSite: 'none', + sameSite: 'none' as 'none', // none 타입으로 지정해줘야 함. maxAge: this.configService.get('COOKIE_EXPIRATION'), - }); + }; + + const cookies = [ + { + name: 'accessToken', + value: accessToken, + options: { ...cookieOptions }, + }, + { + name: 'refreshToken', + value: refreshToken, + options: { ...cookieOptions }, + }, + ]; + + for (const cookie of cookies) { + res.cookie(cookie.name, cookie.value, cookie.options); + } + + // res.cookie('accessToken', accessToken, { + // httpOnly: true, + // secure: true, + // domain: this.configService.get('COOKIE_DOMAIN'), + // sameSite: 'none', + // maxAge: this.configService.get('COOKIE_EXPIRATION'), + // }); res.redirect(this.configService.get('CLIENT_MAIN_PAGE_URL')); }