Skip to content

Commit

Permalink
Fixed share page issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Oct 25, 2022
1 parent fc879bb commit 2290be7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
14 changes: 6 additions & 8 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ export function getAuthToken(req) {

export function getShareToken(req) {
try {
return parseSecureToken(req.headers[SHARE_TOKEN_HEADER], secret());
return parseToken(req.headers[SHARE_TOKEN_HEADER], secret());
} catch {
return null;
}
}

export function isValidToken(token, validation) {
try {
const result = parseToken(token, secret());

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') {
return validation(result);
return validation(token);
}
} catch (e) {
return false;
Expand All @@ -38,7 +36,7 @@ export function isValidToken(token, validation) {
}

export async function allowQuery(req) {
const { id: websiteId } = req.query;
const { id } = req.query;

const { userId, isAdmin, shareToken } = req.auth ?? {};

Expand All @@ -47,11 +45,11 @@ export async function allowQuery(req) {
}

if (shareToken) {
return isValidToken(shareToken, { websiteUuid: websiteId });
return isValidToken(shareToken, { id });
}

if (userId) {
const website = await getWebsite({ websiteUuid: websiteId });
const website = await getWebsite({ id });

return website && website.userId === userId;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const useAuth = createMiddleware(async (req, res, next) => {
const token = await getAuthToken(req);
const shareToken = await getShareToken(req);

if (!token) {
if (!token && !shareToken) {
return unauthorized(res);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "umami",
"version": "1.39.0",
"version": "1.39.1",
"description": "A simple, fast, privacy-focused alternative to Google Analytics.",
"author": "Mike Cao <[email protected]>",
"license": "MIT",
Expand Down
7 changes: 4 additions & 3 deletions pages/api/share/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export default async (req, res) => {
const website = await getWebsiteByShareId(id);

if (website) {
const { websiteId, websiteUuid } = website;
const token = createToken({ websiteId, websiteUuid }, secret());
const { websiteUuid } = website;
const data = { id: websiteUuid };
const token = createToken(data, secret());

return ok(res, { websiteId, websiteUuid, token });
return ok(res, { ...data, token });
}

return notFound(res);
Expand Down
4 changes: 1 addition & 3 deletions pages/share/[...id].js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ export default function SharePage() {
return null;
}

const { websiteUuid } = shareToken;

return (
<Layout>
<WebsiteDetails websiteId={websiteUuid} />
<WebsiteDetails websiteId={shareToken.id} />
</Layout>
);
}

0 comments on commit 2290be7

Please sign in to comment.