From 7e587198dddac20fbb0744adb490797c15f1cafc Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Thu, 1 Jun 2023 12:49:28 -0700 Subject: [PATCH] Add incr on block. --- lib/cache.ts | 6 ++++++ lib/session.ts | 2 ++ 2 files changed, 8 insertions(+) diff --git a/lib/cache.ts b/lib/cache.ts index e63a53bb..7ee7bf28 100644 --- a/lib/cache.ts +++ b/lib/cache.ts @@ -54,6 +54,11 @@ async function fetchUserBlock(userId: string) { return redis.get(key); } +async function incrementUserBlock(userId: string) { + const key = `user:block:${userId}`; + return redis.incr(key); +} + export default { fetchWebsite, storeWebsite, @@ -65,5 +70,6 @@ export default { storeSession, deleteSession, fetchUserBlock, + incrementUserBlock, enabled: redis.enabled, }; diff --git a/lib/session.ts b/lib/session.ts index 1fedb91b..7fa06215 100644 --- a/lib/session.ts +++ b/lib/session.ts @@ -99,6 +99,8 @@ export async function findSession(req: NextApiRequestCollect) { async function checkUserBlock(userId: string) { if (process.env.ENABLE_BLOCKER && (await cache.fetchUserBlock(userId))) { + await cache.incrementUserBlock(userId); + throw new Error('Usage Limit.'); } }