mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Add usage.
This commit is contained in:
parent
345dee5720
commit
2ce62c1023
@ -49,6 +49,11 @@ async function deleteSession(id) {
|
|||||||
return deleteObject(`session:${id}`);
|
return deleteObject(`session:${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchUserBlock(userId: string) {
|
||||||
|
const key = `user:block:${userId}`;
|
||||||
|
return redis.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
fetchWebsite,
|
fetchWebsite,
|
||||||
storeWebsite,
|
storeWebsite,
|
||||||
@ -59,5 +64,6 @@ export default {
|
|||||||
fetchSession,
|
fetchSession,
|
||||||
storeSession,
|
storeSession,
|
||||||
deleteSession,
|
deleteSession,
|
||||||
|
fetchUserBlock,
|
||||||
enabled: redis.enabled,
|
enabled: redis.enabled,
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import { createMiddleware, unauthorized, badRequest, parseSecureToken } from 'next-basics';
|
import {
|
||||||
|
createMiddleware,
|
||||||
|
unauthorized,
|
||||||
|
badRequest,
|
||||||
|
parseSecureToken,
|
||||||
|
tooManyRequest,
|
||||||
|
} from 'next-basics';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { validate } from 'uuid';
|
import { validate } from 'uuid';
|
||||||
@ -30,6 +36,9 @@ export const useSession = createMiddleware(async (req, res, next) => {
|
|||||||
|
|
||||||
(req as any).session = session;
|
(req as any).session = session;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
if (e.message === 'Usage Limit.') {
|
||||||
|
return tooManyRequest(res, e.message);
|
||||||
|
}
|
||||||
return badRequest(res, e.message);
|
return badRequest(res, e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
|
|||||||
import { createSession } from 'queries';
|
import { createSession } from 'queries';
|
||||||
import { validate } from 'uuid';
|
import { validate } from 'uuid';
|
||||||
import { loadSession, loadWebsite } from './query';
|
import { loadSession, loadWebsite } from './query';
|
||||||
|
import cache from './cache';
|
||||||
|
|
||||||
export async function findSession(req: NextApiRequestCollect) {
|
export async function findSession(req: NextApiRequestCollect) {
|
||||||
const { payload } = getJsonBody<CollectRequestBody>(req);
|
const { payload } = getJsonBody<CollectRequestBody>(req);
|
||||||
@ -21,6 +22,8 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||||||
const result = await parseToken(cacheToken, secret());
|
const result = await parseToken(cacheToken, secret());
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
|
await checkUserBlock(result?.ownerId);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,6 +42,8 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||||||
throw new Error(`Website not found: ${websiteId}.`);
|
throw new Error(`Website not found: ${websiteId}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await checkUserBlock(website.userId);
|
||||||
|
|
||||||
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
|
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
|
||||||
await getClientInfo(req, payload);
|
await getClientInfo(req, payload);
|
||||||
const sessionId = uuid(websiteId, hostname, ip, userAgent);
|
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.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ export interface NextApiRequestCollect extends NextApiRequest {
|
|||||||
session: {
|
session: {
|
||||||
id: string;
|
id: string;
|
||||||
websiteId: string;
|
websiteId: string;
|
||||||
|
ownerId: string;
|
||||||
hostname: string;
|
hostname: string;
|
||||||
browser: string;
|
browser: string;
|
||||||
os: string;
|
os: string;
|
||||||
|
Loading…
Reference in New Issue
Block a user