2022-10-06 22:13:44 +02:00
|
|
|
import { parseToken } from 'next-basics';
|
|
|
|
import { validate } from 'uuid';
|
2022-10-07 00:00:16 +02:00
|
|
|
import { secret, uuid } from 'lib/crypto';
|
2022-08-29 22:04:58 +02:00
|
|
|
import redis, { DELETED } from 'lib/redis';
|
2022-10-07 00:00:16 +02:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
2022-08-27 06:10:46 +02:00
|
|
|
import { getClientInfo, getJsonBody } from 'lib/request';
|
2022-10-25 19:45:56 +02:00
|
|
|
import { createSession, getSessionByUuid, getWebsite } from 'queries';
|
2020-07-20 10:54:21 +02:00
|
|
|
|
2020-09-18 07:52:20 +02:00
|
|
|
export async function getSession(req) {
|
2022-03-11 04:01:33 +01:00
|
|
|
const { payload } = getJsonBody(req);
|
2020-08-09 08:48:43 +02:00
|
|
|
|
|
|
|
if (!payload) {
|
|
|
|
throw new Error('Invalid request');
|
|
|
|
}
|
|
|
|
|
2022-03-19 06:26:23 +01:00
|
|
|
const cache = req.headers['x-umami-cache'];
|
2020-10-03 05:33:46 +02:00
|
|
|
|
|
|
|
if (cache) {
|
2022-10-07 00:00:16 +02:00
|
|
|
const result = await parseToken(cache, secret());
|
2020-10-03 05:33:46 +02:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 10:54:21 +02:00
|
|
|
|
2022-10-10 22:42:18 +02:00
|
|
|
const { website: websiteUuid, hostname, screen, language } = payload;
|
2022-08-26 08:12:47 +02:00
|
|
|
|
2022-10-10 22:42:18 +02:00
|
|
|
if (!validate(websiteUuid)) {
|
2022-08-29 05:20:54 +02:00
|
|
|
return null;
|
2020-08-12 05:05:40 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:10:46 +02:00
|
|
|
let websiteId = null;
|
2020-07-23 05:45:09 +02:00
|
|
|
|
2022-10-12 06:48:33 +02:00
|
|
|
// Check if website exists
|
2022-10-07 00:00:16 +02:00
|
|
|
if (redis.enabled) {
|
2022-10-10 22:42:18 +02:00
|
|
|
websiteId = Number(await redis.get(`website:${websiteUuid}`));
|
2022-08-29 22:04:58 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 22:29:55 +02:00
|
|
|
// Check database if does not exists in Redis
|
|
|
|
if (!websiteId) {
|
2022-10-25 19:45:56 +02:00
|
|
|
const website = await getWebsite({ websiteUuid });
|
2022-10-12 08:09:06 +02:00
|
|
|
websiteId = website ? website.id : null;
|
2022-10-06 22:29:55 +02:00
|
|
|
}
|
2022-08-27 06:10:46 +02:00
|
|
|
|
2022-08-29 22:04:58 +02:00
|
|
|
if (!websiteId || websiteId === DELETED) {
|
2022-10-10 22:42:18 +02:00
|
|
|
throw new Error(`Website not found: ${websiteUuid}`);
|
2020-08-21 04:17:27 +02:00
|
|
|
}
|
2020-08-12 05:05:40 +02:00
|
|
|
|
2022-08-26 08:12:47 +02:00
|
|
|
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
|
2022-10-12 08:09:06 +02:00
|
|
|
const sessionUuid = uuid(websiteUuid, hostname, ip, userAgent);
|
2020-08-12 05:05:40 +02:00
|
|
|
|
2022-08-27 06:10:46 +02:00
|
|
|
let sessionId = null;
|
|
|
|
let session = null;
|
2020-08-12 05:05:40 +02:00
|
|
|
|
2022-10-07 00:00:16 +02:00
|
|
|
if (!clickhouse.enabled) {
|
2022-10-06 22:13:44 +02:00
|
|
|
// Check if session exists
|
2022-10-07 00:00:16 +02:00
|
|
|
if (redis.enabled) {
|
2022-10-10 22:42:18 +02:00
|
|
|
sessionId = Number(await redis.get(`session:${sessionUuid}`));
|
2022-10-06 22:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check database if does not exists in Redis
|
|
|
|
if (!sessionId) {
|
2022-10-10 22:42:18 +02:00
|
|
|
session = await getSessionByUuid(sessionUuid);
|
2022-10-12 08:09:06 +02:00
|
|
|
sessionId = session ? session.id : null;
|
2022-10-06 22:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sessionId) {
|
|
|
|
try {
|
|
|
|
session = await createSession(websiteId, {
|
2022-10-10 22:42:18 +02:00
|
|
|
sessionUuid,
|
2022-10-06 22:13:44 +02:00
|
|
|
hostname,
|
|
|
|
browser,
|
|
|
|
os,
|
|
|
|
screen,
|
|
|
|
language,
|
|
|
|
country,
|
|
|
|
device,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.message.toLowerCase().includes('unique constraint')) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session = {
|
2022-10-10 22:42:18 +02:00
|
|
|
sessionId,
|
|
|
|
sessionUuid,
|
2022-10-06 22:13:44 +02:00
|
|
|
hostname,
|
|
|
|
browser,
|
|
|
|
os,
|
|
|
|
screen,
|
|
|
|
language,
|
|
|
|
country,
|
|
|
|
device,
|
|
|
|
};
|
|
|
|
}
|
2020-08-08 02:19:42 +02:00
|
|
|
|
2020-08-21 04:17:27 +02:00
|
|
|
return {
|
2022-10-12 08:09:06 +02:00
|
|
|
website: {
|
|
|
|
websiteId,
|
|
|
|
websiteUuid,
|
|
|
|
},
|
2022-09-12 18:55:34 +02:00
|
|
|
session,
|
2020-08-21 04:17:27 +02:00
|
|
|
};
|
2020-08-05 07:45:05 +02:00
|
|
|
}
|