Fixed issue with accessing user dashboards. Closes #1590

This commit is contained in:
Mike Cao 2022-10-25 15:48:49 -07:00
parent 46a18f2918
commit 8b64ef1a4e
8 changed files with 33 additions and 25 deletions

View File

@ -1,6 +1,5 @@
import { useState } from 'react'; import { useState } from 'react';
import { defineMessages, useIntl } from 'react-intl'; import { defineMessages, useIntl } from 'react-intl';
import { useRouter } from 'next/router';
import Page from 'components/layout/Page'; import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader'; import PageHeader from 'components/layout/PageHeader';
import WebsiteList from 'components/pages/WebsiteList'; import WebsiteList from 'components/pages/WebsiteList';
@ -16,10 +15,7 @@ const messages = defineMessages({
more: { id: 'label.more', defaultMessage: 'More' }, more: { id: 'label.more', defaultMessage: 'More' },
}); });
export default function Dashboard() { export default function Dashboard({ userId }) {
const router = useRouter();
const { id } = router.query;
const userId = id?.[0];
const dashboard = useDashboard(); const dashboard = useDashboard();
const { showCharts, limit, editing } = dashboard; const { showCharts, limit, editing } = dashboard;
const [max, setMax] = useState(limit); const [max, setMax] = useState(limit);

View File

@ -29,13 +29,15 @@ export default function AccountSettings() {
const Checkmark = ({ isAdmin }) => (isAdmin ? <Icon icon={<Check />} size="medium" /> : null); const Checkmark = ({ isAdmin }) => (isAdmin ? <Icon icon={<Check />} size="medium" /> : null);
const DashboardLink = row => ( const DashboardLink = row => {
<Link href={`/dashboard/${row.userId}/${row.username}`}> return (
<a> <Link href={`/dashboard/${row.accountUuid}/${row.username}`}>
<Icon icon={<LinkIcon />} /> <a>
</a> <Icon icon={<LinkIcon />} />
</Link> </a>
); </Link>
);
};
const Buttons = row => ( const Buttons = row => (
<ButtonLayout align="right"> <ButtonLayout align="right">

View File

@ -21,9 +21,9 @@ export default async (req, res) => {
if (req.method === 'POST') { if (req.method === 'POST') {
const { username, password, account_uuid } = req.body; 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'); return badRequest(res, 'Account already exists');
} }

View File

@ -10,7 +10,7 @@ export default async (req, res) => {
if (req.method === 'GET') { if (req.method === 'GET') {
const { userId } = req.auth; const { userId } = req.auth;
const websites = await getUserWebsites(userId); const websites = await getUserWebsites({ userId });
const ids = websites.map(({ websiteUuid }) => websiteUuid); const ids = websites.map(({ websiteUuid }) => websiteUuid);
const token = createToken({ websites: ids }, secret()); const token = createToken({ websites: ids }, secret());
const data = await getRealtimeData(ids, subMinutes(new Date(), 30)); const data = await getRealtimeData(ids, subMinutes(new Date(), 30));

View File

@ -6,15 +6,16 @@ import { uuid } from 'lib/crypto';
export default async (req, res) => { export default async (req, res) => {
await useAuth(req, res); await useAuth(req, res);
const { userId: currentUserId, isAdmin, accountUuid } = req.auth;
const { user_id, include_all } = req.query; const { user_id, include_all } = req.query;
const { userId: currentUserId, isAdmin } = req.auth;
const accountUuid = user_id || req.auth.accountUuid;
let account; let account;
if (accountUuid) { 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 (req.method === 'GET') {
if (userId && userId !== currentUserId && !isAdmin) { if (userId && userId !== currentUserId && !isAdmin) {
@ -24,7 +25,7 @@ export default async (req, res) => {
const websites = const websites =
isAdmin && include_all isAdmin && include_all
? await getAllWebsites() ? await getAllWebsites()
: await getUserWebsites(userId || currentUserId); : await getUserWebsites({ userId: account.id });
return ok(res, websites); return ok(res, websites);
} }

View File

@ -2,17 +2,27 @@ import React from 'react';
import Layout from 'components/layout/Layout'; import Layout from 'components/layout/Layout';
import Dashboard from 'components/pages/Dashboard'; import Dashboard from 'components/pages/Dashboard';
import useRequireLogin from 'hooks/useRequireLogin'; import useRequireLogin from 'hooks/useRequireLogin';
import { useRouter } from 'next/router';
import useUser from 'hooks/useUser';
export default function DashboardPage() { export default function DashboardPage() {
const {
query: { id },
isReady,
asPath,
} = useRouter();
const { loading } = useRequireLogin(); const { loading } = useRequireLogin();
const user = useUser();
if (loading) { if (!user || !isReady || loading) {
return null; return null;
} }
const userId = id?.[0];
return ( return (
<Layout> <Layout>
<Dashboard /> <Dashboard key={asPath} userId={user.id || userId} />
</Layout> </Layout>
); );
} }

View File

@ -14,6 +14,7 @@ export async function getAccounts() {
isAdmin: true, isAdmin: true,
createdAt: true, createdAt: true,
updatedAt: true, updatedAt: true,
accountUuid: true,
}, },
}); });
} }

View File

@ -1,10 +1,8 @@
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function getUserWebsites(userId) { export async function getUserWebsites(where) {
return prisma.client.website.findMany({ return prisma.client.website.findMany({
where: { where,
userId,
},
orderBy: { orderBy: {
name: 'asc', name: 'asc',
}, },