This commit is contained in:
2025-11-24 18:39:57 +03:30
parent 9996ea3b09
commit 2de08b9e65
7 changed files with 44 additions and 33 deletions
+18 -7
View File
@@ -57,7 +57,7 @@ export class TokensService {
return this.em.persistAndFlush(token);
}
async refreshToken(oldRefreshToken: string, isAdmin: boolean) {
async refreshToken(oldRefreshToken: string) {
const token = await this.em.findOne(RefreshToken, { token: oldRefreshToken });
if (!token) throw new UnauthorizedException(AuthMessage.INVALID_REFRESH_TOKEN);
@@ -68,16 +68,27 @@ export class TokensService {
throw new UnauthorizedException(AuthMessage.REFRESH_TOKEN_EXPIRED);
}
// Store token data before removal
const ownerId = token.ownerId;
const restId = token.restId;
// Remove the old token
this.em.remove(token);
const isAdmin = token.type === RefreshTokenType.ADMIN;
try {
// Generate new tokens
const tokens = await this.generateAccessAndRefreshToken(ownerId, restId, isAdmin);
// Generate new tokens within the same transaction
const tokens = await this.generateAccessAndRefreshToken(token.ownerId, token.restId, isAdmin);
// Commit the transaction
await this.em.flush();
// Commit the transaction
await this.em.flush();
return tokens;
return tokens;
} catch (error) {
// If token generation fails, the old token is already removed
// This is acceptable as the token was already used
this.logger.error('Failed to generate new tokens after refresh', error);
throw new UnauthorizedException('Failed to refresh token');
}
}
private generateRefreshToken() {