From 18afe4dc5c1263e681f5ee20b05576c545ff52a7 Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Thu, 12 Dec 2024 13:56:50 -0500 Subject: [PATCH] Updating the loadSession function to redirect user to the default accountList Id if they have an underscore as their accountList ID --- pages/acceptInvite.page.tsx | 4 +- pages/accountLists/[accountListId].page.tsx | 13 +++- .../[accountListId]/coaching.page.tsx | 4 +- .../coaching/[coachingId].page.tsx | 4 +- .../contacts/[[...contactId]].page.tsx | 4 +- .../contacts/flows/setup.page.tsx | 4 +- .../[accountListId]/reports/coaching.page.tsx | 4 +- .../reports/designationAccounts.page.tsx | 4 +- .../donations/[[...contactId]].page.tsx | 4 +- .../[[...contactId]].page.tsx | 4 +- .../[financialAccountId].page.tsx | 4 +- .../[financialAccountId]/entries.page.tsx | 4 +- .../reports/financialAccounts/index.page.tsx | 4 +- .../partnerCurrency/[[...contactId]].page.tsx | 4 +- .../[[...contactId]].page.tsx | 4 +- .../salaryCurrency/[[...contactId]].page.tsx | 4 +- .../settings/integrations/index.page.tsx | 4 +- .../settings/manageAccounts.page.tsx | 4 +- .../settings/manageCoaches.page.tsx | 4 +- .../settings/notifications.page.tsx | 4 +- .../settings/organizations.page.tsx | 4 +- .../organizations/accountLists.page.tsx | 4 +- .../settings/organizations/contacts.page.tsx | 4 +- .../settings/preferences.page.tsx | 4 +- .../[accountListId]/setup/finish.page.tsx | 4 +- .../tasks/[[...contactId]].page.tsx | 4 +- .../[accountListId]/tools.page.tsx | 4 +- .../appeals/appeal/[[...appealId]].page.tsx | 4 +- .../tools/appeals/index.page.tsx | 4 +- .../commitmentInfo/[[...contactId]].page.tsx | 4 +- .../emailAddresses/[[...contactId]].page.tsx | 4 +- .../[[...contactId]].page.tsx | 4 +- .../phoneNumbers/[[...contactId]].page.tsx | 4 +- .../sendNewsletter/[[...contactId]].page.tsx | 4 +- .../[accountListId]/tools/import/csv.page.tsx | 4 +- .../tools/import/google.page.tsx | 4 +- .../[accountListId]/tools/import/tnt.page.tsx | 4 +- .../merge/contacts/[[...contactId]].page.tsx | 4 +- .../merge/people/[[...contactId]].page.tsx | 4 +- pages/api/utils/pagePropsHelpers.ts | 60 +++++++++++++++++-- pages/logout.page.tsx | 4 +- pages/setup/connect.page.tsx | 4 +- pages/setup/start.page.tsx | 4 +- 43 files changed, 149 insertions(+), 88 deletions(-) diff --git a/pages/acceptInvite.page.tsx b/pages/acceptInvite.page.tsx index 689084ae3..b6d0d3f1d 100644 --- a/pages/acceptInvite.page.tsx +++ b/pages/acceptInvite.page.tsx @@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next'; import { SetupPage } from 'src/components/Setup/SetupPage'; import useGetAppSettings from 'src/hooks/useGetAppSettings'; import { useRequiredSession } from 'src/hooks/useRequiredSession'; -import { loadSession } from './api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from './api/utils/pagePropsHelpers'; interface FetchAcceptInviteProps { apiToken: string; @@ -149,6 +149,6 @@ const AcceptInvitePage = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default AcceptInvitePage; diff --git a/pages/accountLists/[accountListId].page.tsx b/pages/accountLists/[accountListId].page.tsx index b4461426d..e629549b8 100644 --- a/pages/accountLists/[accountListId].page.tsx +++ b/pages/accountLists/[accountListId].page.tsx @@ -5,7 +5,10 @@ import { GetDefaultAccountDocument, GetDefaultAccountQuery, } from 'pages/api/getDefaultAccount.generated'; -import { makeGetServerSideProps } from 'pages/api/utils/pagePropsHelpers'; +import { + handleUnderscoreAccountListRedirect, + makeGetServerSideProps, +} from 'pages/api/utils/pagePropsHelpers'; import { logErrorOnRollbar } from 'pages/api/utils/rollBar'; import Dashboard from 'src/components/Dashboard'; import { @@ -81,6 +84,14 @@ const AccountListIdPage = ({ export const getServerSideProps = makeGetServerSideProps( async (session, { query, req }) => { + const underscoreRedirect = await handleUnderscoreAccountListRedirect( + session, + req.url, + ); + if (underscoreRedirect) { + return underscoreRedirect; + } + const ssrClient = makeSsrClient(session.user.apiToken); try { diff --git a/pages/accountLists/[accountListId]/coaching.page.tsx b/pages/accountLists/[accountListId]/coaching.page.tsx index f1048302a..1b1164913 100644 --- a/pages/accountLists/[accountListId]/coaching.page.tsx +++ b/pages/accountLists/[accountListId]/coaching.page.tsx @@ -1,7 +1,7 @@ import Head from 'next/head'; import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { CoachingList } from 'src/components/Coaching/CoachingList'; import Loading from 'src/components/Loading'; import { useAccountListId } from 'src/hooks/useAccountListId'; @@ -26,6 +26,6 @@ const CoachingPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default CoachingPage; diff --git a/pages/accountLists/[accountListId]/coaching/[coachingId].page.tsx b/pages/accountLists/[accountListId]/coaching/[coachingId].page.tsx index 2e04917c8..e23a5ac3f 100644 --- a/pages/accountLists/[accountListId]/coaching/[coachingId].page.tsx +++ b/pages/accountLists/[accountListId]/coaching/[coachingId].page.tsx @@ -2,7 +2,7 @@ import Head from 'next/head'; import { useRouter } from 'next/router'; import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { AccountListTypeEnum, CoachingDetail, @@ -35,6 +35,6 @@ const CoachingPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default CoachingPage; diff --git a/pages/accountLists/[accountListId]/contacts/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/contacts/[[...contactId]].page.tsx index a34b0d66d..6c682bc93 100644 --- a/pages/accountLists/[accountListId]/contacts/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/contacts/[[...contactId]].page.tsx @@ -1,7 +1,7 @@ import Head from 'next/head'; import React, { useContext } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ContactsContext, ContactsType, @@ -72,4 +72,4 @@ const ContactsPage: React.FC = () => ( export default ContactsPage; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; diff --git a/pages/accountLists/[accountListId]/contacts/flows/setup.page.tsx b/pages/accountLists/[accountListId]/contacts/flows/setup.page.tsx index fd13d73e1..535207e69 100644 --- a/pages/accountLists/[accountListId]/contacts/flows/setup.page.tsx +++ b/pages/accountLists/[accountListId]/contacts/flows/setup.page.tsx @@ -8,7 +8,7 @@ import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; import { useTranslation } from 'react-i18next'; import { v4 as uuidv4 } from 'uuid'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { colorMap } from 'src/components/Contacts/ContactFlow/ContactFlow'; import { ContactFlowSetupColumn } from 'src/components/Contacts/ContactFlow/ContactFlowSetup/Column/ContactFlowSetupColumn'; import { UnusedStatusesColumn } from 'src/components/Contacts/ContactFlow/ContactFlowSetup/Column/UnusedStatusesColumn'; @@ -228,6 +228,6 @@ const ContactFlowSetupPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default ContactFlowSetupPage; diff --git a/pages/accountLists/[accountListId]/reports/coaching.page.tsx b/pages/accountLists/[accountListId]/reports/coaching.page.tsx index f571c5a51..4c9d30247 100644 --- a/pages/accountLists/[accountListId]/reports/coaching.page.tsx +++ b/pages/accountLists/[accountListId]/reports/coaching.page.tsx @@ -1,7 +1,7 @@ import Head from 'next/head'; import React, { ReactElement } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { AccountListTypeEnum, CoachingDetail, @@ -32,6 +32,6 @@ const CoachingReportPage = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default CoachingReportPage; diff --git a/pages/accountLists/[accountListId]/reports/designationAccounts.page.tsx b/pages/accountLists/[accountListId]/reports/designationAccounts.page.tsx index a6eed44d1..daa77879f 100644 --- a/pages/accountLists/[accountListId]/reports/designationAccounts.page.tsx +++ b/pages/accountLists/[accountListId]/reports/designationAccounts.page.tsx @@ -3,7 +3,7 @@ import React, { useState } from 'react'; import { Box } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; import { DesignationAccountsReport } from 'src/components/Reports/DesignationAccountsReport/DesignationAccountsReport'; @@ -67,6 +67,6 @@ const DesignationAccountsReportPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default DesignationAccountsReportPage; diff --git a/pages/accountLists/[accountListId]/reports/donations/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/reports/donations/[[...contactId]].page.tsx index 2aa777427..0c95bcf4a 100644 --- a/pages/accountLists/[accountListId]/reports/donations/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/reports/donations/[[...contactId]].page.tsx @@ -4,7 +4,7 @@ import React, { useState } from 'react'; import { Box } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { DynamicContactsRightPanel } from 'src/components/Contacts/ContactsRightPanel/DynamicContactsRightPanel'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; @@ -89,6 +89,6 @@ const DonationsReportPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default DonationsReportPage; diff --git a/pages/accountLists/[accountListId]/reports/expectedMonthlyTotal/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/reports/expectedMonthlyTotal/[[...contactId]].page.tsx index c92d16dd8..6d6fdd9df 100644 --- a/pages/accountLists/[accountListId]/reports/expectedMonthlyTotal/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/reports/expectedMonthlyTotal/[[...contactId]].page.tsx @@ -4,7 +4,7 @@ import React, { ReactElement, useState } from 'react'; import { Box } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { DynamicContactsRightPanel } from 'src/components/Contacts/ContactsRightPanel/DynamicContactsRightPanel'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; @@ -101,6 +101,6 @@ const ExpectedMonthlyTotalReportPage = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default ExpectedMonthlyTotalReportPage; diff --git a/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId].page.tsx b/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId].page.tsx index a87f35d4e..7683ca481 100644 --- a/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId].page.tsx +++ b/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId].page.tsx @@ -2,7 +2,7 @@ import Head from 'next/head'; import React, { useState } from 'react'; import { Box } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; import { AccountSummary } from 'src/components/Reports/FinancialAccountsReport/AccountSummary/AccountSummary'; @@ -61,6 +61,6 @@ const FinancialAccountSummaryPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FinancialAccountSummaryPage; diff --git a/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId]/entries.page.tsx b/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId]/entries.page.tsx index 1a048aa6e..024522b87 100644 --- a/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId]/entries.page.tsx +++ b/pages/accountLists/[accountListId]/reports/financialAccounts/[financialAccountId]/entries.page.tsx @@ -2,7 +2,7 @@ import Head from 'next/head'; import React, { ReactElement, useContext, useMemo } from 'react'; import { Box } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; import { AccountTransactions } from 'src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions'; @@ -168,6 +168,6 @@ const FinancialAccountsPage: React.FC = () => ( ); -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FinancialAccountsPage; diff --git a/pages/accountLists/[accountListId]/reports/financialAccounts/index.page.tsx b/pages/accountLists/[accountListId]/reports/financialAccounts/index.page.tsx index 5a622673e..142e80b27 100644 --- a/pages/accountLists/[accountListId]/reports/financialAccounts/index.page.tsx +++ b/pages/accountLists/[accountListId]/reports/financialAccounts/index.page.tsx @@ -2,7 +2,7 @@ import Head from 'next/head'; import React, { useState } from 'react'; import { Box } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; import { FinancialAccounts } from 'src/components/Reports/FinancialAccountsReport/FinancialAccounts/FinancialAccounts'; @@ -64,6 +64,6 @@ const FinancialAccountsPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FinancialAccountsPage; diff --git a/pages/accountLists/[accountListId]/reports/partnerCurrency/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/reports/partnerCurrency/[[...contactId]].page.tsx index 2ad65a15e..c5d956666 100644 --- a/pages/accountLists/[accountListId]/reports/partnerCurrency/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/reports/partnerCurrency/[[...contactId]].page.tsx @@ -4,7 +4,7 @@ import React, { useState } from 'react'; import { Box } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { DynamicContactsRightPanel } from 'src/components/Contacts/ContactsRightPanel/DynamicContactsRightPanel'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; @@ -90,6 +90,6 @@ const PartnerCurrencyReportPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default PartnerCurrencyReportPage; diff --git a/pages/accountLists/[accountListId]/reports/partnerGivingAnalysis/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/reports/partnerGivingAnalysis/[[...contactId]].page.tsx index cb2a2c9ca..35d439837 100644 --- a/pages/accountLists/[accountListId]/reports/partnerGivingAnalysis/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/reports/partnerGivingAnalysis/[[...contactId]].page.tsx @@ -4,7 +4,7 @@ import React, { useMemo, useRef, useState } from 'react'; import { sortBy } from 'lodash'; import { useTranslation } from 'react-i18next'; import { ReportContactFilterSetInput } from 'pages/api/graphql-rest.page.generated'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ContactsProvider } from 'src/components/Contacts/ContactsContext/ContactsContext'; import { DynamicContactsRightPanel } from 'src/components/Contacts/ContactsRightPanel/DynamicContactsRightPanel'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; @@ -174,6 +174,6 @@ const PartnerGivingAnalysisReportPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default PartnerGivingAnalysisReportPage; diff --git a/pages/accountLists/[accountListId]/reports/salaryCurrency/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/reports/salaryCurrency/[[...contactId]].page.tsx index de9716eb6..e4067613d 100644 --- a/pages/accountLists/[accountListId]/reports/salaryCurrency/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/reports/salaryCurrency/[[...contactId]].page.tsx @@ -4,7 +4,7 @@ import React, { useState } from 'react'; import { Box } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { DynamicContactsRightPanel } from 'src/components/Contacts/ContactsRightPanel/DynamicContactsRightPanel'; import { SidePanelsLayout } from 'src/components/Layouts/SidePanelsLayout'; import Loading from 'src/components/Loading'; @@ -90,6 +90,6 @@ const SalaryCurrencyReportPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default SalaryCurrencyReportPage; diff --git a/pages/accountLists/[accountListId]/settings/integrations/index.page.tsx b/pages/accountLists/[accountListId]/settings/integrations/index.page.tsx index e60ed30c8..944953180 100644 --- a/pages/accountLists/[accountListId]/settings/integrations/index.page.tsx +++ b/pages/accountLists/[accountListId]/settings/integrations/index.page.tsx @@ -2,7 +2,7 @@ import { useRouter } from 'next/router'; import React, { useEffect, useState } from 'react'; import { Button } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ChalklineAccordion } from 'src/components/Settings/integrations/Chalkline/ChalklineAccordion'; import { GoogleAccordion } from 'src/components/Settings/integrations/Google/GoogleAccordion'; import { TheKeyAccordion } from 'src/components/Settings/integrations/Key/TheKeyAccordion'; @@ -120,6 +120,6 @@ const Integrations: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default Integrations; diff --git a/pages/accountLists/[accountListId]/settings/manageAccounts.page.tsx b/pages/accountLists/[accountListId]/settings/manageAccounts.page.tsx index 1e8de017b..cb42c0c7e 100644 --- a/pages/accountLists/[accountListId]/settings/manageAccounts.page.tsx +++ b/pages/accountLists/[accountListId]/settings/manageAccounts.page.tsx @@ -1,7 +1,7 @@ import { useRouter } from 'next/router'; import React, { ReactElement, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ManageAccountAccessAccordion } from 'src/components/Settings/Accounts/ManageAccountAccess/ManageAccountAccessAccordion'; import { MergeAccountsAccordion } from 'src/components/Settings/Accounts/MergeAccounts/MergeAccountsAccordion'; import { MergeSpouseAccountsAccordion } from 'src/components/Settings/Accounts/MergeSpouseAccounts/MergeSpouseAccountsAccordion'; @@ -50,6 +50,6 @@ const ManageAccounts = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default ManageAccounts; diff --git a/pages/accountLists/[accountListId]/settings/manageCoaches.page.tsx b/pages/accountLists/[accountListId]/settings/manageCoaches.page.tsx index f154a57f9..607d2ceca 100644 --- a/pages/accountLists/[accountListId]/settings/manageCoaches.page.tsx +++ b/pages/accountLists/[accountListId]/settings/manageCoaches.page.tsx @@ -1,7 +1,7 @@ import { useRouter } from 'next/router'; import React, { ReactElement, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ManageCoachesAccessAccordion } from 'src/components/Settings/Coaches/ManageCoachesAccess/ManageCoachesAccessAccordion'; import { AccordionGroup } from 'src/components/Shared/Forms/Accordions/AccordionGroup'; import { SettingsWrapper } from './Wrapper'; @@ -36,6 +36,6 @@ const ManageCoaching = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default ManageCoaching; diff --git a/pages/accountLists/[accountListId]/settings/notifications.page.tsx b/pages/accountLists/[accountListId]/settings/notifications.page.tsx index 3a94e0139..f6dfc4e28 100644 --- a/pages/accountLists/[accountListId]/settings/notifications.page.tsx +++ b/pages/accountLists/[accountListId]/settings/notifications.page.tsx @@ -2,7 +2,7 @@ import { useRouter } from 'next/router'; import React from 'react'; import { Box, Button } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { NotificationsTable } from 'src/components/Settings/notifications/NotificationsTable'; import { SetupBanner } from 'src/components/Settings/preferences/SetupBanner'; import { useSetupContext } from 'src/components/Setup/SetupProvider'; @@ -78,6 +78,6 @@ const Notifications: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default Notifications; diff --git a/pages/accountLists/[accountListId]/settings/organizations.page.tsx b/pages/accountLists/[accountListId]/settings/organizations.page.tsx index da5951b0c..c2dbc4039 100644 --- a/pages/accountLists/[accountListId]/settings/organizations.page.tsx +++ b/pages/accountLists/[accountListId]/settings/organizations.page.tsx @@ -3,7 +3,7 @@ import React, { ReactElement, useEffect, useState } from 'react'; import { Autocomplete, Box, Skeleton, TextField } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ImpersonateUserAccordion } from 'src/components/Settings/Organization/ImpersonateUser/ImpersonateUserAccordion'; import { ManageOrganizationAccessAccordion } from 'src/components/Settings/Organization/ManageOrganizationAccess/ManageOrganizationAccessAccordion'; import { AccordionGroup } from 'src/components/Shared/Forms/Accordions/AccordionGroup'; @@ -139,6 +139,6 @@ const Organizations = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default Organizations; diff --git a/pages/accountLists/[accountListId]/settings/organizations/accountLists.page.tsx b/pages/accountLists/[accountListId]/settings/organizations/accountLists.page.tsx index 8efcc3512..46602f767 100644 --- a/pages/accountLists/[accountListId]/settings/organizations/accountLists.page.tsx +++ b/pages/accountLists/[accountListId]/settings/organizations/accountLists.page.tsx @@ -11,7 +11,7 @@ import { import { styled } from '@mui/material/styles'; import useMediaQuery from '@mui/material/useMediaQuery'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { AccountLists } from 'src/components/Settings/Organization/AccountLists/AccountLists'; import { useDebouncedValue } from 'src/hooks/useDebounce'; import { SettingsWrapper } from '../Wrapper'; @@ -143,6 +143,6 @@ const AccountListsOrganizations = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default AccountListsOrganizations; diff --git a/pages/accountLists/[accountListId]/settings/organizations/contacts.page.tsx b/pages/accountLists/[accountListId]/settings/organizations/contacts.page.tsx index 77e9faa26..43ac26f12 100644 --- a/pages/accountLists/[accountListId]/settings/organizations/contacts.page.tsx +++ b/pages/accountLists/[accountListId]/settings/organizations/contacts.page.tsx @@ -11,7 +11,7 @@ import { import { styled } from '@mui/material/styles'; import useMediaQuery from '@mui/material/useMediaQuery'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { Contacts } from 'src/components/Settings/Organization/Contacts/Contacts'; import { useDebouncedValue } from 'src/hooks/useDebounce'; import { SettingsWrapper } from '../Wrapper'; @@ -130,6 +130,6 @@ const OrganizationsContacts = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default OrganizationsContacts; diff --git a/pages/accountLists/[accountListId]/settings/preferences.page.tsx b/pages/accountLists/[accountListId]/settings/preferences.page.tsx index 9733ef0fa..cac9198f1 100644 --- a/pages/accountLists/[accountListId]/settings/preferences.page.tsx +++ b/pages/accountLists/[accountListId]/settings/preferences.page.tsx @@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react'; import { Box, Button, Skeleton } from '@mui/material'; import { styled } from '@mui/material/styles'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { useGetUsersOrganizationsAccountsQuery } from 'src/components/Settings/integrations/Organization/Organizations.generated'; import { useCanUserExportDataQuery, @@ -343,6 +343,6 @@ const Preferences: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default Preferences; diff --git a/pages/accountLists/[accountListId]/setup/finish.page.tsx b/pages/accountLists/[accountListId]/setup/finish.page.tsx index ec69bb5b4..bc211b13f 100644 --- a/pages/accountLists/[accountListId]/setup/finish.page.tsx +++ b/pages/accountLists/[accountListId]/setup/finish.page.tsx @@ -3,7 +3,7 @@ import { useRouter } from 'next/router'; import React, { useEffect } from 'react'; import { Button } from '@mui/material'; import { Trans, useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { SetupPage } from 'src/components/Setup/SetupPage'; import { LargeButton } from 'src/components/Setup/styledComponents'; import { useAccountListId } from 'src/hooks/useAccountListId'; @@ -73,6 +73,6 @@ You can import from software like TntConnect, Google Contacts or a Spreadsheet.` ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FinishPage; diff --git a/pages/accountLists/[accountListId]/tasks/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tasks/[[...contactId]].page.tsx index 156b88239..2f106213f 100644 --- a/pages/accountLists/[accountListId]/tasks/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tasks/[[...contactId]].page.tsx @@ -7,7 +7,7 @@ import { Box, Button, ButtonGroup, Hidden } from '@mui/material'; import { styled } from '@mui/material/styles'; import { DateTime } from 'luxon'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { ContactsProvider } from 'src/components/Contacts/ContactsContext/ContactsContext'; import { DynamicContactsRightPanel } from 'src/components/Contacts/ContactsRightPanel/DynamicContactsRightPanel'; import { InfiniteList } from 'src/components/InfiniteList/InfiniteList'; @@ -411,6 +411,6 @@ const TasksPage: React.FC = () => { //#endregion }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default TasksPage; diff --git a/pages/accountLists/[accountListId]/tools.page.tsx b/pages/accountLists/[accountListId]/tools.page.tsx index 004ea8df9..ba6f3aac8 100644 --- a/pages/accountLists/[accountListId]/tools.page.tsx +++ b/pages/accountLists/[accountListId]/tools.page.tsx @@ -2,7 +2,7 @@ import { useRouter } from 'next/router'; import React, { ReactElement } from 'react'; import { Button } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { SetupBanner } from 'src/components/Settings/preferences/SetupBanner'; import { StickyBox } from 'src/components/Shared/Header/styledComponents'; import ToolsHome from 'src/components/Tool/Home/ToolsHome'; @@ -43,6 +43,6 @@ const ToolsPage = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default ToolsPage; diff --git a/pages/accountLists/[accountListId]/tools/appeals/appeal/[[...appealId]].page.tsx b/pages/accountLists/[accountListId]/tools/appeals/appeal/[[...appealId]].page.tsx index 57d6a7c4c..80f94a33a 100644 --- a/pages/accountLists/[accountListId]/tools/appeals/appeal/[[...appealId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/appeals/appeal/[[...appealId]].page.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import AppealsDetailsPage from 'src/components/Tool/Appeal/AppealDetails/AppealsDetailsPage'; import { ToolsWrapper } from '../../ToolsWrapper'; import { AppealsWrapper } from '../AppealsWrapper'; @@ -29,4 +29,4 @@ const AppealsPage: React.FC = () => ( export default AppealsPage; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; diff --git a/pages/accountLists/[accountListId]/tools/appeals/index.page.tsx b/pages/accountLists/[accountListId]/tools/appeals/index.page.tsx index 9c6791897..f3af95196 100644 --- a/pages/accountLists/[accountListId]/tools/appeals/index.page.tsx +++ b/pages/accountLists/[accountListId]/tools/appeals/index.page.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import AppealsInitialPage from 'src/components/Tool/Appeal/InitialPage/AppealsInitialPage'; import { ToolsWrapper } from '../ToolsWrapper'; @@ -21,4 +21,4 @@ const AppealsPage = (): ReactElement => { export default AppealsPage; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; diff --git a/pages/accountLists/[accountListId]/tools/fix/commitmentInfo/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/fix/commitmentInfo/[[...contactId]].page.tsx index 4cd1fae91..b93b79452 100644 --- a/pages/accountLists/[accountListId]/tools/fix/commitmentInfo/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/fix/commitmentInfo/[[...contactId]].page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import FixCommitmentInfo from 'src/components/Tool/FixCommitmentInfo/FixCommitmentInfo'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -21,6 +21,6 @@ const FixCommitmentInfoPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FixCommitmentInfoPage; diff --git a/pages/accountLists/[accountListId]/tools/fix/emailAddresses/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/fix/emailAddresses/[[...contactId]].page.tsx index 43ab3189b..435338f1c 100644 --- a/pages/accountLists/[accountListId]/tools/fix/emailAddresses/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/fix/emailAddresses/[[...contactId]].page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { FixEmailAddresses } from 'src/components/Tool/FixEmailAddresses/FixEmailAddresses'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -21,6 +21,6 @@ const FixEmailAddressesPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FixEmailAddressesPage; diff --git a/pages/accountLists/[accountListId]/tools/fix/mailingAddresses/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/fix/mailingAddresses/[[...contactId]].page.tsx index e648ba975..8e5c4931a 100644 --- a/pages/accountLists/[accountListId]/tools/fix/mailingAddresses/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/fix/mailingAddresses/[[...contactId]].page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import FixMailingAddresses from 'src/components/Tool/FixMailingAddresses/FixMailingAddresses'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -21,6 +21,6 @@ const FixMailingAddressesPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FixMailingAddressesPage; diff --git a/pages/accountLists/[accountListId]/tools/fix/phoneNumbers/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/fix/phoneNumbers/[[...contactId]].page.tsx index ca7619c2b..f1ce3e9e2 100644 --- a/pages/accountLists/[accountListId]/tools/fix/phoneNumbers/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/fix/phoneNumbers/[[...contactId]].page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import FixPhoneNumbers from 'src/components/Tool/FixPhoneNumbers/FixPhoneNumbers'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -21,6 +21,6 @@ const FixPhoneNumbersPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FixPhoneNumbersPage; diff --git a/pages/accountLists/[accountListId]/tools/fix/sendNewsletter/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/fix/sendNewsletter/[[...contactId]].page.tsx index 3e05d1fdf..952dc668b 100644 --- a/pages/accountLists/[accountListId]/tools/fix/sendNewsletter/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/fix/sendNewsletter/[[...contactId]].page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import FixSendNewsletter from 'src/components/Tool/FixSendNewsletter/FixSendNewsletter'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -21,6 +21,6 @@ const FixSendNewsletterPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default FixSendNewsletterPage; diff --git a/pages/accountLists/[accountListId]/tools/import/csv.page.tsx b/pages/accountLists/[accountListId]/tools/import/csv.page.tsx index dd110199e..eb9eead5d 100644 --- a/pages/accountLists/[accountListId]/tools/import/csv.page.tsx +++ b/pages/accountLists/[accountListId]/tools/import/csv.page.tsx @@ -1,7 +1,7 @@ import { useRouter } from 'next/router'; import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import { CsvImportProvider, CsvImportViewStepEnum, @@ -78,6 +78,6 @@ const CsvHome: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default CsvHome; diff --git a/pages/accountLists/[accountListId]/tools/import/google.page.tsx b/pages/accountLists/[accountListId]/tools/import/google.page.tsx index 197d6a070..f6107fc39 100644 --- a/pages/accountLists/[accountListId]/tools/import/google.page.tsx +++ b/pages/accountLists/[accountListId]/tools/import/google.page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import Loading from 'src/components/Loading'; import GoogleImport from 'src/components/Tool/GoogleImport/GoogleImport'; import { useAccountListId } from 'src/hooks/useAccountListId'; @@ -26,6 +26,6 @@ const GoogleImportPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default GoogleImportPage; diff --git a/pages/accountLists/[accountListId]/tools/import/tnt.page.tsx b/pages/accountLists/[accountListId]/tools/import/tnt.page.tsx index 2cff3e1ef..4f9f5f23f 100644 --- a/pages/accountLists/[accountListId]/tools/import/tnt.page.tsx +++ b/pages/accountLists/[accountListId]/tools/import/tnt.page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import Loading from 'src/components/Loading'; import TntConnect from 'src/components/Tool/TntConnect/TntConnect'; import { useAccountListId } from 'src/hooks/useAccountListId'; @@ -26,6 +26,6 @@ const TntConnectPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default TntConnectPage; diff --git a/pages/accountLists/[accountListId]/tools/merge/contacts/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/merge/contacts/[[...contactId]].page.tsx index 67f9b51f2..c738a4830 100644 --- a/pages/accountLists/[accountListId]/tools/merge/contacts/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/merge/contacts/[[...contactId]].page.tsx @@ -1,7 +1,7 @@ import { useRouter } from 'next/router'; import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import MergeContacts from 'src/components/Tool/MergeContacts/MergeContacts'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -28,6 +28,6 @@ const MergeContactsPage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default MergeContactsPage; diff --git a/pages/accountLists/[accountListId]/tools/merge/people/[[...contactId]].page.tsx b/pages/accountLists/[accountListId]/tools/merge/people/[[...contactId]].page.tsx index 95f9ce294..1960c30dc 100644 --- a/pages/accountLists/[accountListId]/tools/merge/people/[[...contactId]].page.tsx +++ b/pages/accountLists/[accountListId]/tools/merge/people/[[...contactId]].page.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; -import { loadSession } from 'pages/api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from 'pages/api/utils/pagePropsHelpers'; import MergePeople from 'src/components/Tool/MergePeople/MergePeople'; import { ToolsWrapper } from '../../ToolsWrapper'; import { useToolsHelper } from '../../useToolsHelper'; @@ -21,6 +21,6 @@ const MergePeoplePage: React.FC = () => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default MergePeoplePage; diff --git a/pages/api/utils/pagePropsHelpers.ts b/pages/api/utils/pagePropsHelpers.ts index 00f3e503d..546cb6666 100644 --- a/pages/api/utils/pagePropsHelpers.ts +++ b/pages/api/utils/pagePropsHelpers.ts @@ -5,6 +5,11 @@ import { } from 'next'; import { Session } from 'next-auth'; import { getSession } from 'next-auth/react'; +import makeSsrClient from 'src/lib/apollo/ssrClient'; +import { + GetDefaultAccountDocument, + GetDefaultAccountQuery, +} from '../getDefaultAccount.generated'; interface PagePropsWithSession { session: Session; @@ -21,9 +26,9 @@ export const loginRedirect = ( }); // Redirect back to the dashboard if the user isn't an admin -export const enforceAdmin: GetServerSideProps = async ( +export const enforceAdmin = async ( context, -) => { +): Promise> => { const session = await getSession(context); if (!session?.user.admin) { return { @@ -33,6 +38,15 @@ export const enforceAdmin: GetServerSideProps = async ( }, }; } + + const underscoreRedirect = await handleUnderscoreAccountListRedirect( + session, + context.req.url, + ); + if (underscoreRedirect) { + return underscoreRedirect; + } + return { props: { session, @@ -41,12 +55,22 @@ export const enforceAdmin: GetServerSideProps = async ( }; // Redirect back to login screen if user isn't logged in -export const loadSession: GetServerSideProps = async ( +export const ensureSessionAndAccountList = async ( context, -) => { +): Promise> => { const session = await getSession(context); if (!session?.user.apiToken) { - return loginRedirect(context); + return { + ...loginRedirect(context), + }; + } + + const underscoreRedirect = await handleUnderscoreAccountListRedirect( + session, + context.req.url, + ); + if (underscoreRedirect) { + return underscoreRedirect; } return { @@ -56,6 +80,32 @@ export const loadSession: GetServerSideProps = async ( }; }; +export const handleUnderscoreAccountListRedirect = async ( + session: Session, + url?: string, +): Promise | undefined> => { + if (url?.includes('/accountLists/_')) { + // Redirect to the default account list if the "_" is where the account list ID would be in the URL + // This is a common pattern in our app, so we handle it here to avoid repeating + const ssrClient = makeSsrClient(session.user.apiToken); + const { data } = await ssrClient.query({ + query: GetDefaultAccountDocument, + }); + + if (data.user.defaultAccountList) { + return { + redirect: { + destination: url.replace( + '/accountLists/_', + `/accountLists/${data.user.defaultAccountList}`, + ), + permanent: false, + }, + }; + } + } +}; + /** * It is a common pattern in `getServerSideProps` to need to first extract the * API token from the session, redirect to the login page if the API token is diff --git a/pages/logout.page.tsx b/pages/logout.page.tsx index 448272fcc..ee7b20d92 100644 --- a/pages/logout.page.tsx +++ b/pages/logout.page.tsx @@ -8,7 +8,7 @@ import { signOut } from 'next-auth/react'; import { useTranslation } from 'react-i18next'; import useGetAppSettings from 'src/hooks/useGetAppSettings'; import { clearDataDogUser } from 'src/lib/dataDog'; -import { loadSession } from './api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from './api/utils/pagePropsHelpers'; const BoxWrapper = styled(Box)(({ theme }) => ({ backgroundColor: theme.palette.cruGrayLight.main, @@ -51,6 +51,6 @@ const LogoutPage = ({}): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default LogoutPage; diff --git a/pages/setup/connect.page.tsx b/pages/setup/connect.page.tsx index 14cf7ff63..cc6170fe5 100644 --- a/pages/setup/connect.page.tsx +++ b/pages/setup/connect.page.tsx @@ -3,7 +3,7 @@ import React, { ReactElement } from 'react'; import { useTranslation } from 'react-i18next'; import { Connect } from 'src/components/Setup/Connect'; import useGetAppSettings from 'src/hooks/useGetAppSettings'; -import { loadSession } from '../api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from '../api/utils/pagePropsHelpers'; // This is the second page of the setup tour. It lets users connect to // organizations. It will be shown if the user doesn't have any organization @@ -22,6 +22,6 @@ const ConnectPage = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default ConnectPage; diff --git a/pages/setup/start.page.tsx b/pages/setup/start.page.tsx index 60fb266de..0dfcf0871 100644 --- a/pages/setup/start.page.tsx +++ b/pages/setup/start.page.tsx @@ -12,7 +12,7 @@ import { } from 'src/components/Shared/Links/Links'; import useGetAppSettings from 'src/hooks/useGetAppSettings'; import { formatLanguage, languages } from 'src/lib/data/languages'; -import { loadSession } from '../api/utils/pagePropsHelpers'; +import { ensureSessionAndAccountList } from '../api/utils/pagePropsHelpers'; // This is the first page of the tour, and it lets users choose their language. It is always shown. const StartPage = (): ReactElement => { @@ -92,6 +92,6 @@ const StartPage = (): ReactElement => { ); }; -export const getServerSideProps = loadSession; +export const getServerSideProps = ensureSessionAndAccountList; export default StartPage;