mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 12:47:13 +01:00
Refactored session and collect process.
This commit is contained in:
parent
e58aa9e90f
commit
132bbcbe0d
7 changed files with 192 additions and 252 deletions
49
lib/session.js
Normal file
49
lib/session.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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(':');
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue