diff --git a/backend/apps/client/src/auth/auth.controller.ts b/backend/apps/client/src/auth/auth.controller.ts index 5132dd4ef7..c9020ef4ab 100644 --- a/backend/apps/client/src/auth/auth.controller.ts +++ b/backend/apps/client/src/auth/auth.controller.ts @@ -64,8 +64,12 @@ export class AuthController { @Req() req: AuthenticatedRequest, @Res({ passthrough: true }) res: Response ) { + const refreshToken = req.cookies['refresh_token'] + // FIX ME: refreshToken이 없을 때 에러를 던지는 것이 맞는지 확인 + // 일단은 refreshToken이 없을 때는 무시하도록 함 + if (!refreshToken) return try { - await this.authService.deleteRefreshToken(req.user.id) + await this.authService.deleteRefreshToken(req.user.id, refreshToken) res.clearCookie('refresh_token', REFRESH_TOKEN_COOKIE_OPTIONS) } catch (error) { this.logger.error(error) diff --git a/backend/apps/client/src/auth/auth.service.spec.ts b/backend/apps/client/src/auth/auth.service.spec.ts index 323b426d49..fe21dd8f97 100644 --- a/backend/apps/client/src/auth/auth.service.spec.ts +++ b/backend/apps/client/src/auth/auth.service.spec.ts @@ -177,7 +177,7 @@ describe('AuthService', () => { describe('isValidRefreshToken', () => { it("should return true when the given refresh token match with the user's cached refresh token", async () => { //given - stub(cache, 'get').resolves(REFRESH_TOKEN) + stub(cache, 'get').resolves(1) //when const result = await service.isValidRefreshToken(REFRESH_TOKEN, user.id) diff --git a/backend/apps/client/src/auth/auth.service.ts b/backend/apps/client/src/auth/auth.service.ts index 694094c17b..74ca7b153d 100644 --- a/backend/apps/client/src/auth/auth.service.ts +++ b/backend/apps/client/src/auth/auth.service.ts @@ -75,9 +75,9 @@ export class AuthService { async isValidRefreshToken(refreshToken: string, userId: number) { const cachedRefreshToken = await this.cacheManager.get( - refreshTokenCacheKey(userId) + refreshTokenCacheKey(userId, refreshToken) ) - if (cachedRefreshToken !== refreshToken) { + if (cachedRefreshToken !== 1) { return false } return true @@ -92,17 +92,20 @@ export class AuthService { expiresIn: REFRESH_TOKEN_EXPIRE_TIME }) + // userId: refreshToken을 key로 cache에 저장 await this.cacheManager.set( - refreshTokenCacheKey(userId), - refreshToken, + refreshTokenCacheKey(userId, refreshToken), + 1, REFRESH_TOKEN_EXPIRE_TIME * 1000 // milliseconds ) return { accessToken, refreshToken } } - async deleteRefreshToken(userId: number) { - return await this.cacheManager.del(refreshTokenCacheKey(userId)) + async deleteRefreshToken(userId: number, refreshToken: string) { + return await this.cacheManager.del( + refreshTokenCacheKey(userId, refreshToken) + ) } async githubLogin(res: Response, githubUser: GithubUser) { diff --git a/backend/libs/cache/src/keys.ts b/backend/libs/cache/src/keys.ts index f3eac0c104..8d76b46565 100644 --- a/backend/libs/cache/src/keys.ts +++ b/backend/libs/cache/src/keys.ts @@ -1,5 +1,5 @@ -export const refreshTokenCacheKey = (userId: number) => - `user:${userId}:refresh_token` +export const refreshTokenCacheKey = (userId: number, refreshToken: string) => + `user:${userId}:${refreshToken}` export const emailAuthenticationPinCacheKey = (email: string) => `email:${email}:email-auth`