fix: session revoke

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-01-31 11:58:29 +08:00
parent 4c9c5a46cf
commit 2bfc745bf5
3 changed files with 22 additions and 7 deletions

View File

@@ -12,6 +12,8 @@ export const CurrentUser = createParamDecorator(
export const CurrentUserToken = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
return getNestExecutionContextRequest(ctx).token
const token = getNestExecutionContextRequest(ctx).token
return token ? token.replace(/[Bb]earer /, '') : ''
},
)

View File

@@ -158,7 +158,7 @@ export class UserController {
@Delete('/session/all')
@Auth()
async deleteAllSession() {
return this.authService.jwtServicePublic.revokeAll()
async deleteAllSession(@CurrentUserToken() currentToken: string) {
return this.authService.jwtServicePublic.revokeAll([currentToken])
}
}

View File

@@ -92,10 +92,23 @@ export class JWTService {
}
}
async revokeAll() {
const redis = this.cacheService.getClient()
const key = getRedisKey(RedisKeys.JWTStore)
await redis.del(key)
async revokeAll(excludeTokens?: string[]) {
if (Array.isArray(excludeTokens) && excludeTokens.length > 0) {
const redis = this.cacheService.getClient()
const key = getRedisKey(RedisKeys.JWTStore)
const allMd5Tokens = await redis.hkeys(key)
const excludedMd5Tokens = excludeTokens.map((t) => md5(t))
for (const md5Token of allMd5Tokens) {
if (!excludedMd5Tokens.includes(md5Token)) {
await redis.hdel(key, md5Token)
}
}
} else {
const redis = this.cacheService.getClient()
const key = getRedisKey(RedisKeys.JWTStore)
await redis.del(key)
}
}
async storeTokenInRedis(token: string, info?: any) {