Added types for session data.

This commit is contained in:
Mike Cao 2024-04-18 14:23:14 -07:00
parent 32cfb74c49
commit 4ca4be4445
5 changed files with 25 additions and 22 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "2.11.2", "version": "2.12.0",
"description": "A simple, fast, privacy-focused alternative to Google Analytics.", "description": "A simple, fast, privacy-focused alternative to Google Analytics.",
"author": "Umami Software, Inc. <hello@umami.is>", "author": "Umami Software, Inc. <hello@umami.is>",
"license": "MIT", "license": "MIT",

View File

@ -132,7 +132,7 @@ export async function getClientInfo(req: NextApiRequestCollect) {
const subdivision2 = location?.subdivision2; const subdivision2 = location?.subdivision2;
const city = location?.city; const city = location?.city;
const browser = browserName(userAgent); const browser = browserName(userAgent);
const os = detectOS(userAgent); const os = detectOS(userAgent) as string;
const device = getDevice(req.body?.payload?.screen, os); const device = getDevice(req.body?.payload?.screen, os);
return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device }; return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device };

View File

@ -4,7 +4,7 @@ import redis from '@umami/redis-client';
import { getAuthToken, parseShareToken } from 'lib/auth'; import { getAuthToken, parseShareToken } from 'lib/auth';
import { ROLES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import { findSession } from 'lib/session'; import { getSession } from 'lib/session';
import { import {
badRequest, badRequest,
createMiddleware, createMiddleware,
@ -27,7 +27,7 @@ export const useCors = createMiddleware(
export const useSession = createMiddleware(async (req, res, next) => { export const useSession = createMiddleware(async (req, res, next) => {
try { try {
const session = await findSession(req as NextApiRequestCollect); const session = await getSession(req as NextApiRequestCollect);
if (!session) { if (!session) {
log('useSession: Session not found'); log('useSession: Session not found');

View File

@ -6,23 +6,9 @@ import { createSession } from 'queries';
import cache from './cache'; import cache from './cache';
import clickhouse from './clickhouse'; import clickhouse from './clickhouse';
import { loadSession, loadWebsite } from './load'; import { loadSession, loadWebsite } from './load';
import { SessionData } from 'lib/types';
export async function findSession(req: NextApiRequestCollect): Promise<{ export async function getSession(req: NextApiRequestCollect): Promise<SessionData> {
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;
}> {
const { payload } = req.body; const { payload } = req.body;
if (!payload) { if (!payload) {
@ -35,6 +21,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{
if (cacheToken) { if (cacheToken) {
const result = await parseToken(cacheToken, secret()); const result = await parseToken(cacheToken, secret());
// Token is valid
if (result) { if (result) {
await checkUserBlock(result?.ownerId); await checkUserBlock(result?.ownerId);
@ -45,7 +32,6 @@ export async function findSession(req: NextApiRequestCollect): Promise<{
// Verify payload // Verify payload
const { website: websiteId, hostname, screen, language } = payload; const { website: websiteId, hostname, screen, language } = payload;
// Check the hostname value for legality to eliminate dirty data
const validHostnameRegex = /^[\w-.]+$/; const validHostnameRegex = /^[\w-.]+$/;
if (!validHostnameRegex.test(hostname)) { if (!validHostnameRegex.test(hostname)) {
throw new Error('Invalid hostname.'); throw new Error('Invalid hostname.');
@ -78,7 +64,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{
visitId, visitId,
hostname, hostname,
browser, browser,
os: os as any, os,
device, device,
screen, screen,
language, language,

View File

@ -230,3 +230,20 @@ export interface RealtimeData {
countries?: any[]; countries?: any[];
visitors?: 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;
}