Add usage.

This commit is contained in:
Brian Cao 2023-05-15 20:41:12 -07:00
parent 345dee5720
commit 2ce62c1023
4 changed files with 29 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
import { createSession } from 'queries';
import { validate } from 'uuid';
import { loadSession, loadWebsite } from './query';
import cache from './cache';
export async function findSession(req: NextApiRequestCollect) {
const { payload } = getJsonBody<CollectRequestBody>(req);
@ -21,6 +22,8 @@ export async function findSession(req: NextApiRequestCollect) {
const result = await parseToken(cacheToken, secret());
if (result) {
await checkUserBlock(result?.ownerId);
return result;
}
}
@ -39,6 +42,8 @@ export async function findSession(req: NextApiRequestCollect) {
throw new Error(`Website not found: ${websiteId}.`);
}
await checkUserBlock(website.userId);
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req, payload);
const sessionId = uuid(websiteId, hostname, ip, userAgent);
@ -88,5 +93,11 @@ export async function findSession(req: NextApiRequestCollect) {
}
}
return session;
return { ...session, ownerId: website.userId };
}
async function checkUserBlock(userId: string) {
if (process.env.ENABLE_BLOCKER && (await cache.fetchUserBlock(userId))) {
throw new Error('Usage Limit.');
}
}