Added CORS middleware. Updated umami script.

This commit is contained in:
Mike Cao 2020-07-18 10:36:46 -07:00
parent bb04015b46
commit 58a1c63407
10 changed files with 144 additions and 78 deletions

View file

@ -1,11 +1,16 @@
import { getWebsite, getSession, createSession } from 'lib/db';
import { hash, parseSessionRequest } from 'lib/utils';
import { allowPost } from 'lib/middleware';
export default async (req, res) => {
let result = { time: Date.now() };
await allowPost(req, res);
let result = { success: 0, time: Date.now() };
const {
website_id,
session_id,
hostname,
browser,
os,
screen,
@ -13,24 +18,32 @@ export default async (req, res) => {
country,
} = await parseSessionRequest(req);
const website = await getWebsite(website_id);
if (website_id && session_id) {
const website = await getWebsite(website_id);
if (website) {
const session = await getSession(session_id);
if (website) {
const session = await getSession(session_id);
if (!session) {
await createSession(website_id, session_id, { browser, os, screen, language, country });
if (!session) {
await createSession(website_id, session_id, {
hostname,
browser,
os,
screen,
language,
country,
});
}
result = {
...result,
success: 1,
session_id,
website_id,
hash: hash(`${website_id}${session_id}${result.time}`),
};
}
result = {
...result,
session_id,
website_id,
hash: hash(`${website_id}${session_id}${result.time}`),
};
}
res.setHeader('Access-Control-Allow-Origin', '*');
res.status(200).json(result);
};