fix bugs
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user