diff --git a/apps/core/src/modules/user/user.controller.ts b/apps/core/src/modules/user/user.controller.ts index e7ac4c4c..c33d8b6c 100644 --- a/apps/core/src/modules/user/user.controller.ts +++ b/apps/core/src/modules/user/user.controller.ts @@ -53,13 +53,14 @@ export class UserController { @CurrentUser() user: UserDocument, @CurrentUserToken() token: string, ) { - await this.authService.jwtServicePublic.revokeToken(token) await this.userService.recordFootstep(ipLocation.ip) return { - token: this.authService.jwtServicePublic.sign(user.id, { - ip: ipLocation.ip, - ua: ipLocation.agent, - }), + token: await this.authService.jwtServicePublic + .sign(user.id, { + ip: ipLocation.ip, + ua: ipLocation.agent, + }) + .then(() => this.authService.jwtServicePublic.revokeToken(token, 6000)), } } @@ -73,7 +74,7 @@ export class UserController { const avatar = user.avatar ?? getAvatar(mail) return { - token: this.authService.jwtServicePublic.sign(user.id, { + token: await this.authService.jwtServicePublic.sign(user.id, { ip: ipLocation.ip, ua: ipLocation.agent, }), diff --git a/apps/core/src/modules/user/user.service.ts b/apps/core/src/modules/user/user.service.ts index 98c53ca7..02b030b0 100644 --- a/apps/core/src/modules/user/user.service.ts +++ b/apps/core/src/modules/user/user.service.ts @@ -81,7 +81,7 @@ export class UserService { } const res = await this.userModel.create({ ...model }) - const token = this.authService.jwtServicePublic.sign(res.id) + const token = await this.authService.jwtServicePublic.sign(res.id) return { token, username: res.username } } diff --git a/apps/core/src/processors/helper/helper.jwt.service.ts b/apps/core/src/processors/helper/helper.jwt.service.ts index 55b8128e..aa384731 100644 --- a/apps/core/src/processors/helper/helper.jwt.service.ts +++ b/apps/core/src/processors/helper/helper.jwt.service.ts @@ -72,13 +72,23 @@ export class JWTService { }) } - async revokeToken(token: string) { + async revokeToken(token: string, delay?: number) { const redis = this.cacheService.getClient() const key = getRedisKey(RedisKeys.JWTStore) - await redis.hdel( - key, - token.startsWith(`jwt-`) ? token.replace(`jwt-`, '') : md5(token), - ) + if (delay) { + // FIXME + setTimeout(() => { + redis.hdel( + key, + token.startsWith(`jwt-`) ? token.replace(`jwt-`, '') : md5(token), + ) + }, delay) + } else { + await redis.hdel( + key, + token.startsWith(`jwt-`) ? token.replace(`jwt-`, '') : md5(token), + ) + } } async revokeAll() { @@ -101,11 +111,11 @@ export class JWTService { public static readonly expiresDay = SECURITY.jwtExpire - sign(id: string, info?: { ip: string; ua: string }) { + async sign(id: string, info?: { ip: string; ua: string }) { const token = sign({ id }, this.secret, { expiresIn: `${JWTService.expiresDay}d`, }) - this.storeTokenInRedis(token, info || {}) + await this.storeTokenInRedis(token, info || {}) return token } }