Skip to content

Commit

Permalink
Merge pull request #4178 from mozilla/check-session-diagnostics
Browse files Browse the repository at this point in the history
MNTOR-2925 - add a function for checking the session and logging more…
  • Loading branch information
mansaj authored Feb 8, 2024
2 parents 3e91b91 + 173343a commit 9d1f12f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("/");
}

Expand Down
24 changes: 24 additions & 0 deletions src/app/functions/server/checkSession.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9d1f12f

Please sign in to comment.