Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added types for session data.

Mike Cao4ca4be4445b5
diff --git a/package.json b/package.json
index ad2bc79d2..c72a054f7 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. <[email protected]>",
"license": "MIT",
diff --git a/src/lib/detect.ts b/src/lib/detect.ts
index 25dee386b..64f70e6ca 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 6885de908..8145ac116 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 5f3020e17..1f87ffcce 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<SessionData> {
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 5c9a39c6e..27db220f0 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;
+}