umami/pages/api/session.js

52 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-07-17 10:03:38 +02:00
import { getWebsite, getSession, createSession } from 'lib/db';
import { hash, parseSessionRequest } from 'lib/utils';
import { allowPost } from 'lib/middleware';
2020-07-17 10:03:38 +02:00
export default async (req, res) => {
await allowPost(req, res);
2020-07-19 07:51:17 +02:00
let result = { success: 0 };
2020-07-18 09:25:29 +02:00
const {
website_id,
session_id,
hostname,
2020-07-18 09:25:29 +02:00
browser,
os,
screen,
language,
country,
} = await parseSessionRequest(req);
2020-07-17 10:03:38 +02:00
if (website_id && session_id) {
const website = await getWebsite(website_id);
2020-07-17 10:03:38 +02:00
if (website) {
const session = await getSession(session_id);
2020-07-19 07:51:17 +02:00
const time = Date.now();
2020-07-17 10:03:38 +02:00
if (!session) {
await createSession(website_id, session_id, {
hostname,
browser,
os,
screen,
language,
country,
});
}
2020-07-17 10:03:38 +02:00
result = {
...result,
success: 1,
session_id,
website_id,
2020-07-19 07:51:17 +02:00
time,
hash: hash(`${website_id}${session_id}${time}`),
};
}
2020-07-17 10:03:38 +02:00
}
res.status(200).json(result);
};