Fixed share page issue.

This commit is contained in:
Mike Cao 2022-10-24 19:48:10 -07:00
parent fc879bbb7d
commit 2290be7ca4
5 changed files with 13 additions and 16 deletions

View File

@ -15,7 +15,7 @@ export function getAuthToken(req) {
export function getShareToken(req) { export function getShareToken(req) {
try { try {
return parseSecureToken(req.headers[SHARE_TOKEN_HEADER], secret()); return parseToken(req.headers[SHARE_TOKEN_HEADER], secret());
} catch { } catch {
return null; return null;
} }
@ -23,12 +23,10 @@ export function getShareToken(req) {
export function isValidToken(token, validation) { export function isValidToken(token, validation) {
try { try {
const result = parseToken(token, secret());
if (typeof validation === 'object') { if (typeof validation === 'object') {
return !Object.keys(validation).find(key => result[key] !== validation[key]); return !Object.keys(validation).find(key => token[key] !== validation[key]);
} else if (typeof validation === 'function') { } else if (typeof validation === 'function') {
return validation(result); return validation(token);
} }
} catch (e) { } catch (e) {
return false; return false;
@ -38,7 +36,7 @@ export function isValidToken(token, validation) {
} }
export async function allowQuery(req) { export async function allowQuery(req) {
const { id: websiteId } = req.query; const { id } = req.query;
const { userId, isAdmin, shareToken } = req.auth ?? {}; const { userId, isAdmin, shareToken } = req.auth ?? {};
@ -47,11 +45,11 @@ export async function allowQuery(req) {
} }
if (shareToken) { if (shareToken) {
return isValidToken(shareToken, { websiteUuid: websiteId }); return isValidToken(shareToken, { id });
} }
if (userId) { if (userId) {
const website = await getWebsite({ websiteUuid: websiteId }); const website = await getWebsite({ id });
return website && website.userId === userId; return website && website.userId === userId;
} }

View File

@ -29,7 +29,7 @@ export const useAuth = createMiddleware(async (req, res, next) => {
const token = await getAuthToken(req); const token = await getAuthToken(req);
const shareToken = await getShareToken(req); const shareToken = await getShareToken(req);
if (!token) { if (!token && !shareToken) {
return unauthorized(res); return unauthorized(res);
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "1.39.0", "version": "1.39.1",
"description": "A simple, fast, privacy-focused alternative to Google Analytics.", "description": "A simple, fast, privacy-focused alternative to Google Analytics.",
"author": "Mike Cao <mike@mikecao.com>", "author": "Mike Cao <mike@mikecao.com>",
"license": "MIT", "license": "MIT",

View File

@ -9,10 +9,11 @@ export default async (req, res) => {
const website = await getWebsiteByShareId(id); const website = await getWebsiteByShareId(id);
if (website) { if (website) {
const { websiteId, websiteUuid } = website; const { websiteUuid } = website;
const token = createToken({ websiteId, websiteUuid }, secret()); const data = { id: websiteUuid };
const token = createToken(data, secret());
return ok(res, { websiteId, websiteUuid, token }); return ok(res, { ...data, token });
} }
return notFound(res); return notFound(res);

View File

@ -14,11 +14,9 @@ export default function SharePage() {
return null; return null;
} }
const { websiteUuid } = shareToken;
return ( return (
<Layout> <Layout>
<WebsiteDetails websiteId={websiteUuid} /> <WebsiteDetails websiteId={shareToken.id} />
</Layout> </Layout>
); );
} }