Skip to content

Commit

Permalink
feat(be): implement the management of the user's sessions when using …
Browse files Browse the repository at this point in the history
…multiple clients (#1387)

* feat(be): implement the management of the user's sessions, using multiple clients

* fix(be): fix the wrong logic of checking the specific refresh token in the refresh token list

* test(be): fix the service test code

* fix(be): delete the unnecessary comment

* fix(be): fix the auth service code

* fix(be): fix the auth controller

* Revert "fix(be): fix the auth controller"

This reverts commit c3853dd.

* Revert "test(be): fix the service test code"

This reverts commit c2704c1.

* revert: this reverts commits from e661643 to 08fb6ad

* fix(be): change the refresh token cache key

* fix(be): change the params of refresh token cache key func and logic of cache validation

* fix(be): add the refresh token param to logout endpoint

* test(be): fix the service test code

* fix(be): fix the logout logic when the refresh token is non-existent
  • Loading branch information
gyunseo authored Feb 22, 2024
1 parent 8b8ad04 commit 963bf6b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
6 changes: 5 additions & 1 deletion backend/apps/client/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/client/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions backend/apps/client/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions backend/libs/cache/src/keys.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down

0 comments on commit 963bf6b

Please sign in to comment.