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 04cbc9b0..29ff694f 100644 --- a/lib/session.ts +++ b/lib/session.ts @@ -80,6 +80,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.'); } }