From 8b64ef1a4e84ce004807c550c53c87eede7559e7 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 25 Oct 2022 15:48:49 -0700 Subject: [PATCH] Fixed issue with accessing user dashboards. Closes #1590 --- components/pages/Dashboard.js | 6 +----- components/settings/AccountSettings.js | 16 +++++++++------- pages/api/accounts/index.js | 4 ++-- pages/api/realtime/init.js | 2 +- pages/api/websites/index.js | 9 +++++---- pages/dashboard/[[...id]].js | 14 ++++++++++++-- queries/admin/account/getAccounts.js | 1 + queries/admin/website/getUserWebsites.js | 6 ++---- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/components/pages/Dashboard.js b/components/pages/Dashboard.js index bd6a8b3a..70a4d624 100644 --- a/components/pages/Dashboard.js +++ b/components/pages/Dashboard.js @@ -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'; @@ -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); diff --git a/components/settings/AccountSettings.js b/components/settings/AccountSettings.js index 2ec378f1..eebdae40 100644 --- a/components/settings/AccountSettings.js +++ b/components/settings/AccountSettings.js @@ -29,13 +29,15 @@ export default function AccountSettings() { const Checkmark = ({ isAdmin }) => (isAdmin ? } size="medium" /> : null); - const DashboardLink = row => ( - - - } /> - - - ); + const DashboardLink = row => { + return ( + + + } /> + + + ); + }; const Buttons = row => ( diff --git a/pages/api/accounts/index.js b/pages/api/accounts/index.js index 5addd63a..22248ed4 100644 --- a/pages/api/accounts/index.js +++ b/pages/api/accounts/index.js @@ -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'); } diff --git a/pages/api/realtime/init.js b/pages/api/realtime/init.js index a7e175a5..9a9a4297 100644 --- a/pages/api/realtime/init.js +++ b/pages/api/realtime/init.js @@ -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)); diff --git a/pages/api/websites/index.js b/pages/api/websites/index.js index 9fade8c2..36d38660 100644 --- a/pages/api/websites/index.js +++ b/pages/api/websites/index.js @@ -6,15 +6,16 @@ import { uuid } from 'lib/crypto'; 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) { @@ -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); } diff --git a/pages/dashboard/[[...id]].js b/pages/dashboard/[[...id]].js index 452a425e..7c762097 100644 --- a/pages/dashboard/[[...id]].js +++ b/pages/dashboard/[[...id]].js @@ -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 ( - + ); } diff --git a/queries/admin/account/getAccounts.js b/queries/admin/account/getAccounts.js index 71d5e4c5..ceca2582 100644 --- a/queries/admin/account/getAccounts.js +++ b/queries/admin/account/getAccounts.js @@ -14,6 +14,7 @@ export async function getAccounts() { isAdmin: true, createdAt: true, updatedAt: true, + accountUuid: true, }, }); } diff --git a/queries/admin/website/getUserWebsites.js b/queries/admin/website/getUserWebsites.js index c1a9d559..9a725ec2 100644 --- a/queries/admin/website/getUserWebsites.js +++ b/queries/admin/website/getUserWebsites.js @@ -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', },