refactor: atualizações e remoção de módulos não utilizados
This commit is contained in:
@@ -24,18 +24,21 @@ export class RefreshTokenService {
|
||||
private readonly jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async generateRefreshToken(userId: number, sessionId?: string): Promise<string> {
|
||||
async generateRefreshToken(
|
||||
userId: number,
|
||||
sessionId?: string,
|
||||
): Promise<string> {
|
||||
const tokenId = randomBytes(32).toString('hex');
|
||||
const refreshToken = this.jwtService.sign(
|
||||
{ userId, tokenId, sessionId, type: 'refresh' },
|
||||
{ expiresIn: '7d' }
|
||||
{ expiresIn: '7d' },
|
||||
);
|
||||
|
||||
const tokenData: RefreshTokenData = {
|
||||
userId,
|
||||
tokenId,
|
||||
sessionId,
|
||||
expiresAt: DateUtil.nowTimestamp() + (this.REFRESH_TOKEN_TTL * 1000),
|
||||
expiresAt: DateUtil.nowTimestamp() + this.REFRESH_TOKEN_TTL * 1000,
|
||||
createdAt: DateUtil.nowTimestamp(),
|
||||
};
|
||||
|
||||
@@ -50,7 +53,7 @@ export class RefreshTokenService {
|
||||
async validateRefreshToken(refreshToken: string): Promise<JwtPayload> {
|
||||
try {
|
||||
const decoded = this.jwtService.verify(refreshToken) as any;
|
||||
|
||||
|
||||
if (decoded.type !== 'refresh') {
|
||||
throw new UnauthorizedException('Token inválido');
|
||||
}
|
||||
@@ -68,14 +71,14 @@ export class RefreshTokenService {
|
||||
throw new UnauthorizedException('Refresh token expirado');
|
||||
}
|
||||
|
||||
return {
|
||||
id: userId,
|
||||
sellerId: 0,
|
||||
storeId: '',
|
||||
username: '',
|
||||
return {
|
||||
id: userId,
|
||||
sellerId: 0,
|
||||
storeId: '',
|
||||
username: '',
|
||||
email: '',
|
||||
sessionId: sessionId || tokenData.sessionId,
|
||||
tokenId
|
||||
tokenId,
|
||||
} as JwtPayload;
|
||||
} catch (error) {
|
||||
throw new UnauthorizedException('Refresh token inválido');
|
||||
@@ -90,7 +93,7 @@ export class RefreshTokenService {
|
||||
async revokeAllRefreshTokens(userId: number): Promise<void> {
|
||||
const pattern = this.buildRefreshTokenPattern(userId);
|
||||
const keys = await this.redis.keys(pattern);
|
||||
|
||||
|
||||
if (keys.length > 0) {
|
||||
await this.redis.del(...keys);
|
||||
}
|
||||
@@ -99,9 +102,9 @@ export class RefreshTokenService {
|
||||
async getActiveRefreshTokens(userId: number): Promise<RefreshTokenData[]> {
|
||||
const pattern = this.buildRefreshTokenPattern(userId);
|
||||
const keys = await this.redis.keys(pattern);
|
||||
|
||||
|
||||
const tokens: RefreshTokenData[] = [];
|
||||
|
||||
|
||||
for (const key of keys) {
|
||||
const tokenData = await this.redis.get<RefreshTokenData>(key);
|
||||
if (tokenData && tokenData.expiresAt > DateUtil.nowTimestamp()) {
|
||||
@@ -114,11 +117,11 @@ export class RefreshTokenService {
|
||||
|
||||
private async limitRefreshTokensPerUser(userId: number): Promise<void> {
|
||||
const activeTokens = await this.getActiveRefreshTokens(userId);
|
||||
|
||||
|
||||
if (activeTokens.length > this.MAX_REFRESH_TOKENS_PER_USER) {
|
||||
const tokensToRemove = activeTokens
|
||||
.slice(this.MAX_REFRESH_TOKENS_PER_USER)
|
||||
.map(token => token.tokenId);
|
||||
.map((token) => token.tokenId);
|
||||
|
||||
for (const tokenId of tokensToRemove) {
|
||||
await this.revokeRefreshToken(userId, tokenId);
|
||||
|
||||
Reference in New Issue
Block a user