Removed session code.

This commit is contained in:
Mike Cao 2020-08-20 19:17:27 -07:00
parent 343e59e6ff
commit 5cade59111
6 changed files with 47 additions and 53 deletions

View File

@ -68,13 +68,13 @@ export async function getCountry(req, ip) {
lookup.close(); lookup.close();
return result.country.iso_code; return result?.country?.iso_code;
} }
export async function getClientInfo(req, { screen }) { export async function getClientInfo(req, { screen }) {
const userAgent = req.headers['user-agent'];
const ip = getIpAddress(req); const ip = getIpAddress(req);
const country = await getCountry(req, ip); const country = await getCountry(req, ip);
const userAgent = req.headers['user-agent'];
const browser = browserName(userAgent); const browser = browserName(userAgent);
const os = detectOS(userAgent); const os = detectOS(userAgent);
const device = getDevice(screen, browser, os); const device = getDevice(screen, browser, os);

View File

@ -1,6 +1,6 @@
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries'; import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries';
import { getClientInfo } from 'lib/request'; import { getClientInfo } from 'lib/request';
import { uuid, isValidId, parseToken } from 'lib/crypto'; import { uuid, isValidId } from 'lib/crypto';
export async function verifySession(req) { export async function verifySession(req) {
const { payload } = req.body; const { payload } = req.body;
@ -9,50 +9,42 @@ export async function verifySession(req) {
throw new Error('Invalid request'); throw new Error('Invalid request');
} }
const { website: website_uuid, hostname, screen, language, session } = payload; const { website: website_uuid, hostname, screen, language } = payload;
if (!isValidId(website_uuid)) { if (!isValidId(website_uuid)) {
throw new Error(`Invalid website: ${website_uuid}`); throw new Error(`Invalid website: ${website_uuid}`);
} }
const token = await parseToken(session); const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
if (!token || token.website_uuid !== website_uuid) { const website = await getWebsiteByUuid(website_uuid);
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
const website = await getWebsiteByUuid(website_uuid); if (!website) {
throw new Error(`Website not found: ${website_uuid}`);
if (!website) {
throw new Error(`Website not found: ${website_uuid}`);
}
const { website_id } = website;
const session_uuid = uuid(website_id, hostname, ip, userAgent, os);
let session = await getSessionByUuid(session_uuid);
if (!session) {
session = await createSession(website_id, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
});
}
const { session_id } = session;
return {
website_id,
website_uuid,
session_id,
session_uuid,
};
} }
return token; const { website_id } = website;
const session_uuid = uuid(website_id, hostname, ip, userAgent, os);
let session = await getSessionByUuid(session_uuid);
if (!session) {
session = await createSession(website_id, {
session_uuid,
hostname,
browser,
os,
screen,
language,
country,
device,
});
}
const { session_id } = session;
return {
website_id,
session_id,
};
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "0.10.4", "version": "0.11.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ", "description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <mike@mikecao.com>", "author": "Mike Cao <mike@mikecao.com>",
"license": "MIT", "license": "MIT",

View File

@ -1,16 +1,15 @@
import { savePageView, saveEvent } from 'lib/queries'; import { savePageView, saveEvent } from 'lib/queries';
import { useCors, useSession } from 'lib/middleware'; import { useCors, useSession } from 'lib/middleware';
import { createToken } from 'lib/crypto';
import { ok, badRequest } from 'lib/response'; import { ok, badRequest } from 'lib/response';
export default async (req, res) => { export default async (req, res) => {
await useCors(req, res); await useCors(req, res);
await useSession(req, res); await useSession(req, res);
const { session } = req;
const token = await createToken(session);
const { website_id, session_id } = session;
const { type, payload } = req.body; const { type, payload } = req.body;
const {
session: { website_id, session_id },
} = req;
if (type === 'pageview') { if (type === 'pageview') {
const { url, referrer } = payload; const { url, referrer } = payload;
@ -24,5 +23,5 @@ export default async (req, res) => {
return badRequest(res); return badRequest(res);
} }
return ok(res, { session: token }); return ok(res);
}; };

File diff suppressed because one or more lines are too long

View File

@ -2,18 +2,20 @@ import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill'; import 'unfetch/polyfill';
import { post, hook } from '../lib/web'; import { post, hook } from '../lib/web';
((window, sessionKey) => { (window => {
const { const {
screen: { width, height }, screen: { width, height },
navigator: { language }, navigator: { language },
location: { hostname, pathname, search }, location: { hostname, pathname, search },
localStorage: store,
document, document,
history, history,
} = window; } = window;
const script = document.querySelector('script[data-website-id]'); const script = document.querySelector('script[data-website-id]');
const website = script && script.getAttribute('data-website-id');
if (!script) return;
const website = script.getAttribute('data-website-id');
const hostUrl = new URL(script.src).origin; const hostUrl = new URL(script.src).origin;
const screen = `${width}x${height}`; const screen = `${width}x${height}`;
const listeners = []; const listeners = [];
@ -21,9 +23,10 @@ import { post, hook } from '../lib/web';
let currentUrl = `${pathname}${search}`; let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer; let currentRef = document.referrer;
/* Collect metrics */
const collect = (type, params) => { const collect = (type, params) => {
const payload = { const payload = {
session: store.getItem(sessionKey),
url: currentUrl, url: currentUrl,
referrer: currentRef, referrer: currentRef,
website, website,
@ -41,7 +44,7 @@ import { post, hook } from '../lib/web';
return post(`${hostUrl}/api/collect`, { return post(`${hostUrl}/api/collect`, {
type, type,
payload, payload,
}).then(({ session }) => session && store.setItem(sessionKey, session)); });
}; };
const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300)); const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300));
@ -86,4 +89,4 @@ import { post, hook } from '../lib/web';
/* Start */ /* Start */
pageView(); pageView();
})(window, 'umami.session'); })(window);