fix: revoke session in shared cache before database to keep replicas consistent

This commit is contained in:
2026-11-01 14:57:41 +03:00
parent e9bc51288b
commit 53035d475f
4 changed files with 64 additions and 25 deletions
+8 -1
View File
@@ -99,8 +99,15 @@ export class AuthService {
}
async revoke(tokenHash: string): Promise<void> {
const marked = await this.cache.setStrict(
cacheKey(tokenHash),
REVOKED,
CACHE_TTL_MS,
);
if (!marked) {
throw new ApiError('unavailable', 'session cache unavailable');
}
await this.prisma.session.deleteMany({ where: { tokenHash } });
await this.cache.set(cacheKey(tokenHash), REVOKED, CACHE_TTL_MS);
}
}
+16
View File
@@ -66,6 +66,22 @@ export class CacheService implements OnModuleDestroy {
}
}
async setStrict(key: string, value: string, ttlMs: number): Promise<boolean> {
if (!this.client) {
return true;
}
if (!this.trusted) {
return false;
}
try {
await this.client.set(key, value, 'PX', ttlMs);
return true;
} catch {
this.distrust();
return false;
}
}
onModuleDestroy(): void {
this.client?.disconnect();
}
+7 -1
View File
@@ -184,7 +184,13 @@ export const OPERATION_EXAMPLES: Record<string, OperationExamples> = {
'DELETE /api/v1/auth/sessions/current': {
responses: {
'401': UNAUTHORIZED,
'503': UNAVAILABLE,
'503': {
...UNAVAILABLE,
cache: example(
'Redis недоступен — отзыв не выполнен, повторите позже',
error('unavailable', 'session cache unavailable'),
),
},
},
},
'GET /api/v1/users/me': {