From 4ca4be4445b5b42f2585d3e84fcd67eebac35f7a Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Thu, 18 Apr 2024 14:23:14 -0700 Subject: [PATCH] Added types for session data. --- package.json | 2 +- src/lib/detect.ts | 2 +- src/lib/middleware.ts | 4 ++-- src/lib/session.ts | 22 ++++------------------ src/lib/types.ts | 17 +++++++++++++++++ 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index ad2bc79d..c72a054f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umami", - "version": "2.11.2", + "version": "2.12.0", "description": "A simple, fast, privacy-focused alternative to Google Analytics.", "author": "Umami Software, Inc. ", "license": "MIT", diff --git a/src/lib/detect.ts b/src/lib/detect.ts index 25dee386..64f70e6c 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -132,7 +132,7 @@ export async function getClientInfo(req: NextApiRequestCollect) { const subdivision2 = location?.subdivision2; const city = location?.city; const browser = browserName(userAgent); - const os = detectOS(userAgent); + const os = detectOS(userAgent) as string; const device = getDevice(req.body?.payload?.screen, os); return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device }; diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 6885de90..8145ac11 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -4,7 +4,7 @@ import redis from '@umami/redis-client'; import { getAuthToken, parseShareToken } from 'lib/auth'; import { ROLES } from 'lib/constants'; import { secret } from 'lib/crypto'; -import { findSession } from 'lib/session'; +import { getSession } from 'lib/session'; import { badRequest, createMiddleware, @@ -27,7 +27,7 @@ export const useCors = createMiddleware( export const useSession = createMiddleware(async (req, res, next) => { try { - const session = await findSession(req as NextApiRequestCollect); + const session = await getSession(req as NextApiRequestCollect); if (!session) { log('useSession: Session not found'); diff --git a/src/lib/session.ts b/src/lib/session.ts index 5f3020e1..1f87ffcc 100644 --- a/src/lib/session.ts +++ b/src/lib/session.ts @@ -6,23 +6,9 @@ import { createSession } from 'queries'; import cache from './cache'; import clickhouse from './clickhouse'; import { loadSession, loadWebsite } from './load'; +import { SessionData } from 'lib/types'; -export async function findSession(req: NextApiRequestCollect): Promise<{ - id: any; - websiteId: string; - visitId: string; - hostname: string; - browser: string; - os: any; - device: string; - screen: string; - language: string; - country: any; - subdivision1: any; - subdivision2: any; - city: any; - ownerId: string; -}> { +export async function getSession(req: NextApiRequestCollect): Promise { const { payload } = req.body; if (!payload) { @@ -35,6 +21,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{ if (cacheToken) { const result = await parseToken(cacheToken, secret()); + // Token is valid if (result) { await checkUserBlock(result?.ownerId); @@ -45,7 +32,6 @@ export async function findSession(req: NextApiRequestCollect): Promise<{ // Verify payload const { website: websiteId, hostname, screen, language } = payload; - // Check the hostname value for legality to eliminate dirty data const validHostnameRegex = /^[\w-.]+$/; if (!validHostnameRegex.test(hostname)) { throw new Error('Invalid hostname.'); @@ -78,7 +64,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{ visitId, hostname, browser, - os: os as any, + os, device, screen, language, diff --git a/src/lib/types.ts b/src/lib/types.ts index 5c9a39c6..27db220f 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -230,3 +230,20 @@ export interface RealtimeData { countries?: any[]; visitors?: any[]; } + +export interface SessionData { + id: string; + websiteId: string; + visitId: string; + hostname: string; + browser: string; + os: string; + device: string; + screen: string; + language: string; + country: string; + subdivision1: string; + subdivision2: string; + city: string; + ownerId: string; +}