From 173343ae3e115efafddd7a297d63ff86fda51e72 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Thu, 8 Feb 2024 15:08:40 -0800 Subject: [PATCH] MNTOR-2925 - add a function for checking the session and logging more diagnostics --- .../welcome-to-plus/page.tsx | 3 ++- .../user/(dashboard)/dashboard/page.tsx | 3 ++- src/app/functions/server/checkSession.ts | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/app/functions/server/checkSession.ts diff --git a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/welcome-to-plus/page.tsx b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/welcome-to-plus/page.tsx index 23854cce934..3ffe1f7b41b 100644 --- a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/welcome-to-plus/page.tsx +++ b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/welcome-to-plus/page.tsx @@ -17,12 +17,13 @@ import { activateAndOptoutProfile } from "../../../../../../../../../functions/s import { logger } from "../../../../../../../../../functions/server/logging"; import { getL10n } from "../../../../../../../../../functions/server/l10n"; import { refreshStoredScanResults } from "../../../../../../../../../functions/server/refreshStoredScanResults"; +import { checkSession } from "../../../../../../../../../functions/server/checkSession"; export default async function WelcomeToPlusPage() { const session = await getServerSession(authOptions); // Ensure user is logged in - if (!session?.user?.subscriber?.id) { + if (!checkSession(session) || !session?.user?.subscriber?.id) { redirect("/user/dashboard/"); } diff --git a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/page.tsx b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/page.tsx index 06ad7d2b521..a3b8888057f 100644 --- a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/page.tsx +++ b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/page.tsx @@ -34,10 +34,11 @@ import { getEnabledFeatureFlags } from "../../../../../../../db/tables/featureFl import { parseIso8601Datetime } from "../../../../../../../utils/parse"; import { getAttributionsFromCookiesOrDb } from "../../../../../../functions/server/attributions"; import { getUserId } from "../../../../../../functions/server/getUserId"; +import { checkSession } from "../../../../../../functions/server/checkSession"; export default async function DashboardPage() { const session = await getServerSession(authOptions); - if (!session?.user?.subscriber?.id) { + if (!checkSession(session) || !session?.user?.subscriber?.id) { return redirect("/"); } diff --git a/src/app/functions/server/checkSession.ts b/src/app/functions/server/checkSession.ts new file mode 100644 index 00000000000..7dbdfd4c2fb --- /dev/null +++ b/src/app/functions/server/checkSession.ts @@ -0,0 +1,24 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { Session } from "next-auth"; +import { logger } from "./logging"; + +export function checkSession(session: Session | null) { + if (!session) { + logger.warn("no_session"); + return false; + } else if (!session.user) { + logger.warn("no_user_in_session", { session }); + return false; + } else if (!session.user.subscriber) { + logger.warn("no_subscriber_in_session", { session }); + return false; + } else if (!session.user.subscriber.id) { + logger.warn("no_subscriber_id_in_session", { session }); + return false; + } else { + return true; + } +}