@@ -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,
|
||||
}),
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user