mirror of
https://github.com/kremalicious/umami.git
synced 2024-07-01 06:02:02 +02:00
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
|
import { getWebsite, getSession, createSession } from 'lib/db';
|
||
|
import { getCountry, getDevice, getIpAddress, hash, isValidSession } from 'lib/utils';
|
||
|
|
||
|
export default async function checkSession(req) {
|
||
|
const { payload } = req.body;
|
||
|
const { session } = payload;
|
||
|
|
||
|
if (isValidSession(session)) {
|
||
|
return session;
|
||
|
}
|
||
|
|
||
|
const ip = getIpAddress(req);
|
||
|
const { userAgent, browser, os } = getDevice(req);
|
||
|
const country = await getCountry(req, ip);
|
||
|
const { website: website_uuid, hostname, screen, language } = payload;
|
||
|
|
||
|
if (website_uuid) {
|
||
|
const website = await getWebsite(website_uuid);
|
||
|
|
||
|
if (website) {
|
||
|
const { website_id } = website;
|
||
|
const session_uuid = hash(`${website_id}${hostname}${ip}${userAgent}${os}`);
|
||
|
|
||
|
let session = await getSession(session_uuid);
|
||
|
|
||
|
if (!session) {
|
||
|
session = await createSession(website_id, {
|
||
|
session_uuid,
|
||
|
hostname,
|
||
|
browser,
|
||
|
os,
|
||
|
screen,
|
||
|
language,
|
||
|
country,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const { session_id } = session;
|
||
|
|
||
|
return [
|
||
|
website_id,
|
||
|
website_uuid,
|
||
|
session_id,
|
||
|
session_uuid,
|
||
|
hash(website_id, website_uuid, session_id, session_uuid),
|
||
|
].join(':');
|
||
|
}
|
||
|
}
|
||
|
}
|