umami/lib/session.ts

118 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-12-31 22:42:03 +01:00
import { Session, Team, Website } from '@prisma/client';
import cache from 'lib/cache';
2022-11-09 00:50:34 +01:00
import clickhouse from 'lib/clickhouse';
2022-12-31 22:42:03 +01:00
import { secret, uuid } from 'lib/crypto';
2022-12-28 05:20:44 +01:00
import { getClientInfo, getJsonBody } from 'lib/detect';
2022-12-31 22:42:03 +01:00
import { parseToken } from 'next-basics';
import { createSession, getSession, getWebsite } from 'queries';
2022-12-31 22:42:03 +01:00
import { validate } from 'uuid';
2022-12-31 22:42:03 +01:00
export async function findSession(req): Promise<{
error?: {
status: number;
message: string;
};
session?: {
id: string;
websiteId: string;
hostname: string;
browser: string;
os: string;
device: string;
screen: string;
language: string;
country: string;
};
website?: Website & { team?: Team };
}> {
2022-03-11 04:01:33 +01:00
const { payload } = getJsonBody(req);
2020-08-09 08:48:43 +02:00
if (!payload) {
return null;
2020-08-09 08:48:43 +02:00
}
// Verify payload
2022-11-01 07:42:37 +01:00
const { website: websiteId, hostname, screen, language } = payload;
2022-08-26 08:12:47 +02:00
// Find website
2022-12-31 22:42:03 +01:00
let website: Website & { team?: Team } = null;
2022-08-29 22:04:58 +02:00
if (cache.enabled) {
website = await cache.fetchWebsite(websiteId);
} else {
website = await getWebsite({ id: websiteId });
}
2022-12-07 03:36:41 +01:00
if (!website || website.deletedAt) {
2022-11-01 07:42:37 +01:00
throw new Error(`Website not found: ${websiteId}`);
2020-08-21 04:17:27 +02:00
}
2020-08-12 05:05:40 +02:00
2022-12-31 22:42:03 +01:00
// Check if cache token is passed
const cacheToken = req.headers['x-umami-cache'];
if (cacheToken) {
const result = await parseToken(cacheToken, secret());
if (result) {
return { session: result, website };
}
}
if (!validate(websiteId)) {
return null;
}
2022-08-26 08:12:47 +02:00
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
2022-11-01 07:42:37 +01:00
const sessionId = uuid(websiteId, hostname, ip, userAgent);
2020-08-12 05:05:40 +02:00
2022-11-09 02:11:08 +01:00
// Clickhouse does not require session lookup
if (clickhouse.enabled) {
return {
2022-12-31 22:42:03 +01:00
session: {
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
},
website,
2022-11-09 00:50:34 +01:00
};
}
2022-11-09 02:11:08 +01:00
// Find session
2022-12-31 22:42:03 +01:00
let session: Session;
2022-11-09 02:11:08 +01:00
if (cache.enabled) {
session = await cache.fetchSession(sessionId);
} else {
session = await getSession({ id: sessionId });
}
// Create a session if not found
if (!session) {
try {
session = await createSession({
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
});
2022-12-31 22:42:03 +01:00
} catch (e: any) {
2022-11-09 02:11:08 +01:00
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
2022-12-31 22:42:03 +01:00
return { session, website };
2020-08-05 07:45:05 +02:00
}