From 22dd87d5450aa42b5d920e50fa8d8bb9a7f482d3 Mon Sep 17 00:00:00 2001 From: jinddings Date: Tue, 19 Nov 2024 15:21:38 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix=20:=20cookie=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=9C=20accessToken=20validate(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/auth.controller.ts | 2 +- BE/src/auth/strategy/jwt.strategy.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts index 8fcdf300..02c5198c 100644 --- a/BE/src/auth/auth.controller.ts +++ b/BE/src/auth/auth.controller.ts @@ -47,7 +47,7 @@ export class AuthController { @Get('/test') @UseGuards(AuthGuard('jwt')) test(@Req() req: Request) { - return req; + return 'test'; } @ApiOperation({ summary: 'Kakao 로그인 API' }) diff --git a/BE/src/auth/strategy/jwt.strategy.ts b/BE/src/auth/strategy/jwt.strategy.ts index 7f8f5d2a..78063d1f 100644 --- a/BE/src/auth/strategy/jwt.strategy.ts +++ b/BE/src/auth/strategy/jwt.strategy.ts @@ -5,6 +5,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { UserRepository } from '../user.repository'; import { User } from '../user.entity'; +import { Request } from 'express'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { @@ -14,7 +15,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) { ) { super({ secretOrKey: configService.get('JWT_SECRET'), - jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), + jwtFromRequest: extractJWTFromCookie, }); } @@ -31,3 +32,10 @@ export class JwtStrategy extends PassportStrategy(Strategy) { }; } } + +function extractJWTFromCookie(req: Request): string | null { + if (req.cookies && 'accessToken' in req.cookies) { + return req.cookies['accessToken']; + } + return null; +}