forked from mtusi2026/mtusi2026lab3
fix: revoke session in shared cache before database to keep replicas consistent
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vendored
+16
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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': {
|
||||
|
||||
Reference in New Issue
Block a user