umami/pages/api/session.js

37 lines
805 B
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';
export default async (req, res) => {
let result = { time: Date.now() };
2020-07-18 09:25:29 +02:00
const {
website_id,
session_id,
browser,
os,
screen,
language,
country,
} = await parseSessionRequest(req);
2020-07-17 10:03:38 +02:00
const website = await getWebsite(website_id);
if (website) {
const session = await getSession(session_id);
if (!session) {
await createSession(website_id, session_id, { browser, os, screen, language, country });
}
result = {
...result,
session_id,
website_id,
hash: hash(`${website_id}${session_id}${result.time}`),
};
}
2020-07-18 12:33:33 +02:00
res.setHeader('Access-Control-Allow-Origin', '*');
2020-07-17 10:03:38 +02:00
res.status(200).json(result);
};