Skip to content

Commit

Permalink
Fixed issue with accessing user dashboards. Closes umami-software#1590
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao authored and dannyhines committed Nov 9, 2022
1 parent ea70e03 commit 5fc0fce
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 25 deletions.
6 changes: 1 addition & 5 deletions components/pages/Dashboard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { useRouter } from 'next/router';
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import WebsiteList from 'components/pages/WebsiteList';
Expand All @@ -16,10 +15,7 @@ const messages = defineMessages({
more: { id: 'label.more', defaultMessage: 'More' },
});

export default function Dashboard() {
const router = useRouter();
const { id } = router.query;
const userId = id?.[0];
export default function Dashboard({ userId }) {
const dashboard = useDashboard();
const { showCharts, limit, editing } = dashboard;
const [max, setMax] = useState(limit);
Expand Down
16 changes: 9 additions & 7 deletions components/settings/AccountSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ export default function AccountSettings() {

const Checkmark = ({ isAdmin }) => (isAdmin ? <Icon icon={<Check />} size="medium" /> : null);

const DashboardLink = row => (
<Link href={`/dashboard/${row.userId}/${row.username}`}>
<a>
<Icon icon={<LinkIcon />} />
</a>
</Link>
);
const DashboardLink = row => {
return (
<Link href={`/dashboard/${row.accountUuid}/${row.username}`}>
<a>
<Icon icon={<LinkIcon />} />
</a>
</Link>
);
};

const Buttons = row => (
<ButtonLayout align="right">
Expand Down
4 changes: 2 additions & 2 deletions pages/api/accounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export default async (req, res) => {
if (req.method === 'POST') {
const { username, password, account_uuid } = req.body;

const accountByUsername = await getAccount({ username });
const account = await getAccount({ username });

if (accountByUsername) {
if (account) {
return badRequest(res, 'Account already exists');
}

Expand Down
2 changes: 1 addition & 1 deletion pages/api/realtime/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async (req, res) => {
if (req.method === 'GET') {
const { userId } = req.auth;

const websites = await getUserWebsites(userId);
const websites = await getUserWebsites({ userId });
const ids = websites.map(({ websiteUuid }) => websiteUuid);
const token = createToken({ websites: ids }, secret());
const data = await getRealtimeData(ids, subMinutes(new Date(), 30));
Expand Down
9 changes: 5 additions & 4 deletions pages/api/websites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { ok, methodNotAllowed, unauthorized } from 'next-basics';
export default async (req, res) => {
await useAuth(req, res);

const { userId: currentUserId, isAdmin, accountUuid } = req.auth;
const { user_id, include_all } = req.query;
const { userId: currentUserId, isAdmin } = req.auth;
const accountUuid = user_id || req.auth.accountUuid;
let account;

if (accountUuid) {
account = await getAccount({ accountUuid: accountUuid });
account = await getAccount({ accountUuid });
}

const userId = account ? account.id : +user_id;
const userId = account ? account.id : user_id;

if (req.method === 'GET') {
if (userId && userId !== currentUserId && !isAdmin) {
Expand All @@ -24,7 +25,7 @@ export default async (req, res) => {
const websites =
isAdmin && include_all
? await getAllWebsites()
: await getUserWebsites(userId || currentUserId);
: await getUserWebsites({ userId: account.id });

return ok(res, websites);
}
Expand Down
14 changes: 12 additions & 2 deletions pages/dashboard/[[...id]].js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ import React from 'react';
import Layout from 'components/layout/Layout';
import Dashboard from 'components/pages/Dashboard';
import useRequireLogin from 'hooks/useRequireLogin';
import { useRouter } from 'next/router';
import useUser from 'hooks/useUser';

export default function DashboardPage() {
const {
query: { id },
isReady,
asPath,
} = useRouter();
const { loading } = useRequireLogin();
const user = useUser();

if (loading) {
if (!user || !isReady || loading) {
return null;
}

const userId = id?.[0];

return (
<Layout>
<Dashboard />
<Dashboard key={asPath} userId={user.id || userId} />
</Layout>
);
}
1 change: 1 addition & 0 deletions queries/admin/account/getAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function getAccounts() {
isAdmin: true,
createdAt: true,
updatedAt: true,
accountUuid: true,
},
});
}
6 changes: 2 additions & 4 deletions queries/admin/website/getUserWebsites.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import prisma from 'lib/prisma';

export async function getUserWebsites(userId) {
export async function getUserWebsites(where) {
return prisma.client.website.findMany({
where: {
userId,
},
where,
orderBy: {
name: 'asc',
},
Expand Down

0 comments on commit 5fc0fce

Please sign in to comment.