Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNTOR-2925 - add a function for checking the session and logging more… #4178

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading