From 183a6476e7dab90249ac113865ec7b51af1fd997 Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Mon, 10 Mar 2025 09:06:23 +0000 Subject: [PATCH] Merged in fix/check-valid-session (pull request #1502) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix/check valid session * fix:session - check if valid session * Refactor Approved-by: Joakim Jäderberg --- .../server/routers/navigation/mypages/index.ts | 4 +++- apps/scandic-web/server/routers/user/query.ts | 11 ++++++----- apps/scandic-web/utils/session.ts | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/scandic-web/server/routers/navigation/mypages/index.ts b/apps/scandic-web/server/routers/navigation/mypages/index.ts index 63983279e..709960837 100644 --- a/apps/scandic-web/server/routers/navigation/mypages/index.ts +++ b/apps/scandic-web/server/routers/navigation/mypages/index.ts @@ -4,6 +4,8 @@ import { z } from "zod" import { Lang } from "@/constants/languages" import { safeProtectedProcedure } from "@/server/trpc" +import { isValidSession } from "@/utils/session" + import { getVerifiedUser } from "../../user/query" import { getPrimaryLinks } from "./getPrimaryLinks" import { getSecondaryLinks } from "./getSecondaryLinks" @@ -24,7 +26,7 @@ export const myPagesNavigation = safeProtectedProcedure primaryLinks: MyPagesLink[] secondaryLinks: MyPagesLink[] } | null> => { - if (!ctx.session) { + if (!isValidSession(ctx.session)) { return null } diff --git a/apps/scandic-web/server/routers/user/query.ts b/apps/scandic-web/server/routers/user/query.ts index 95d9e03c2..ea389f4de 100644 --- a/apps/scandic-web/server/routers/user/query.ts +++ b/apps/scandic-web/server/routers/user/query.ts @@ -11,6 +11,7 @@ import { import { cache } from "@/utils/cache" import * as maskValue from "@/utils/maskValue" +import { isValidSession } from "@/utils/session" import { getFriendsMembership, getMembershipCards } from "@/utils/user" import { @@ -309,7 +310,7 @@ export const userQueryRouter = router({ return parsedUser(data.data, ctx.isMFA) }), getSafely: safeProtectedProcedure.query(async function getUser({ ctx }) { - if (!ctx.session) { + if (!isValidSession(ctx.session)) { return null } @@ -322,7 +323,7 @@ export const userQueryRouter = router({ return parsedUser(data.data, true) }), name: safeProtectedProcedure.query(async function ({ ctx }) { - if (!ctx.session) { + if (!isValidSession(ctx.session)) { return null } const verifiedData = await getVerifiedUser({ session: ctx.session }) @@ -345,7 +346,7 @@ export const userQueryRouter = router({ return membershipLevel }), safeMembershipLevel: safeProtectedProcedure.query(async function ({ ctx }) { - if (!ctx.session) { + if (!isValidSession(ctx.session)) { return null } const verifiedData = await getVerifiedUser({ session: ctx.session }) @@ -362,7 +363,7 @@ export const userQueryRouter = router({ loginStatus: "Non-logged in", } - if (!ctx.session) { + if (!isValidSession(ctx.session)) { return notLoggedInUserTrackingData } const verifiedUserData = await getVerifiedUser({ session: ctx.session }) @@ -761,7 +762,7 @@ export const userQueryRouter = router({ safePaymentCards: safeProtectedProcedure .input(getSavedPaymentCardsInput) .query(async function ({ ctx, input }) { - if (!ctx.session) { + if (!isValidSession(ctx.session)) { return null } diff --git a/apps/scandic-web/utils/session.ts b/apps/scandic-web/utils/session.ts index d42c157ea..fc818a24b 100644 --- a/apps/scandic-web/utils/session.ts +++ b/apps/scandic-web/utils/session.ts @@ -2,7 +2,7 @@ import "server-only" import type { Session } from "next-auth" -export function isValidSession(session: Session | null) { +export function isValidSession(session: Session | null): session is Session { if (!session) { console.log("No session available (user not authenticated).") return false