fix: invalid website in cache

This commit is contained in:
Allan Jerjas 2024-12-22 16:13:19 +01:00
parent bce70c1034
commit afe559f6ae

View file

@ -18,10 +18,16 @@ export async function getSession(req: NextApiRequestCollect): Promise<SessionDat
const cacheToken = req.headers['x-umami-cache']; const cacheToken = req.headers['x-umami-cache'];
if (cacheToken) { if (cacheToken) {
const result = await parseToken(cacheToken, secret()); const result: SessionData | undefined = await parseToken(cacheToken, secret());
// Token is valid // Token is valid
if (result) { if (result) {
// website may differ in payload
if (result.websiteId !== payload.website) {
await checkWebsite(payload.website);
result.websiteId = payload.website;
}
return result; return result;
} }
} }
@ -29,12 +35,8 @@ export async function getSession(req: NextApiRequestCollect): Promise<SessionDat
// Verify payload // Verify payload
const { website: websiteId, hostname, screen, language } = payload; const { website: websiteId, hostname, screen, language } = payload;
// Find website // check website
const website = await fetchWebsite(websiteId); await checkWebsite(websiteId);
if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
}
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } = const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req); await getClientInfo(req);
@ -90,3 +92,12 @@ export async function getSession(req: NextApiRequestCollect): Promise<SessionDat
return { ...session, visitId }; return { ...session, visitId };
} }
const checkWebsite = async (websiteId: string) => {
// Find website
const website = await fetchWebsite(websiteId);
if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
}
};