umami/lib/session.js

95 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-08-29 05:20:54 +02:00
import { parseToken } from 'next-basics';
import { validate } from 'uuid';
import { uuid } from 'lib/crypto';
2022-08-29 22:04:58 +02:00
import redis, { DELETED } from 'lib/redis';
import { getClientInfo, getJsonBody } from 'lib/request';
import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries';
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) {
const result = await parseToken(cache);
if (result) {
return result;
}
}
2022-08-26 08:12:47 +02:00
const { website: website_uuid, hostname, screen, language } = payload;
2022-08-29 05:20:54 +02:00
if (!validate(website_uuid)) {
return null;
2020-08-12 05:05:40 +02:00
}
let websiteId = null;
2020-07-23 05:45:09 +02:00
// Check if website exists
2022-08-29 22:04:58 +02:00
if (redis.client) {
2022-08-30 01:46:28 +02:00
websiteId = await redis.client.get(`website:${website_uuid}`);
2022-08-29 22:04:58 +02:00
}
// Check database if redis does not have
if (!websiteId) {
const { website_id } = await getWebsiteByUuid(website_uuid);
websiteId = website_id;
}
2022-08-29 22:04:58 +02:00
if (!websiteId || websiteId === DELETED) {
2020-08-21 04:17:27 +02:00
throw new Error(`Website not found: ${website_uuid}`);
}
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);
const session_uuid = uuid(websiteId, hostname, ip, userAgent);
2020-08-12 05:05:40 +02:00
let sessionCreated = false;
let sessionId = null;
let session = null;
2020-08-12 05:05:40 +02:00
// Check if session exists
2022-08-29 22:04:58 +02:00
if (redis.client) {
2022-08-29 19:47:01 +02:00
sessionCreated = !!(await redis.client.get(`session:${session_uuid}`));
2022-08-29 22:04:58 +02:00
}
// Check database if redis does not have
if (!sessionCreated) {
session = await getSessionByUuid(session_uuid);
sessionCreated = !!session;
sessionId = session ? session.session_id : null;
}
2022-08-26 07:04:32 +02:00
if (!sessionCreated) {
2022-01-06 10:21:05 +01:00
try {
2022-08-29 19:47:01 +02:00
session = await createSession(BigInt(websiteId), {
2022-01-06 10:21:05 +01:00
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
});
sessionId = session ? session.session_id : null;
2022-01-06 10:21:05 +01:00
} catch (e) {
if (!e.message.toLowerCase().includes('unique constraint')) {
2022-01-06 10:21:05 +01:00
throw e;
}
}
}
2020-08-21 04:17:27 +02:00
return {
website_id: websiteId,
session_id: sessionId,
2022-07-22 23:43:19 +02:00
session_uuid,
2020-08-21 04:17:27 +02:00
};
2020-08-05 07:45:05 +02:00
}