From 711bf4b2d38dd7fbb5d464edce7059503d9fe154 Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Tue, 17 Dec 2024 15:15:55 +0100 Subject: [PATCH] feat/valid-session: check valid user/session from token instead of making a slow request to api --- .../SignUpVerification/index.tsx | 7 ++++--- .../DynamicContent/SignupFormWrapper/index.tsx | 7 ++++--- components/Sidebar/JoinLoyalty/index.tsx | 8 +++++--- components/Sidebar/MyPagesNavigation.tsx | 10 ++++------ utils/session.ts | 18 ++++++++++++++++++ 5 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 utils/session.ts diff --git a/components/Blocks/DynamicContent/SignUpVerification/index.tsx b/components/Blocks/DynamicContent/SignUpVerification/index.tsx index 4ae9fd899..d178e067a 100644 --- a/components/Blocks/DynamicContent/SignUpVerification/index.tsx +++ b/components/Blocks/DynamicContent/SignUpVerification/index.tsx @@ -1,11 +1,12 @@ import { redirect } from "next/navigation" import { overview } from "@/constants/routes/myPages" -import { getProfileSafely } from "@/lib/trpc/memoizedRequests" +import { auth } from "@/auth" import LoginButton from "@/components/LoginButton" import { getIntl } from "@/i18n" import { getLang } from "@/i18n/serverContext" +import { isValidSession } from "@/utils/session" import styles from "./signUpVerification.module.css" @@ -14,8 +15,8 @@ import type { SignUpVerificationProps } from "@/types/components/blocks/dynamicC export default async function SignUpVerification({ dynamic_content, }: SignUpVerificationProps) { - const user = await getProfileSafely() - if (user) { + const session = await auth() + if (isValidSession(session)) { redirect(overview[getLang()]) } const intl = await getIntl() diff --git a/components/Blocks/DynamicContent/SignupFormWrapper/index.tsx b/components/Blocks/DynamicContent/SignupFormWrapper/index.tsx index e93785b0e..c0c875b79 100644 --- a/components/Blocks/DynamicContent/SignupFormWrapper/index.tsx +++ b/components/Blocks/DynamicContent/SignupFormWrapper/index.tsx @@ -1,18 +1,19 @@ import { redirect } from "next/navigation" import { overview } from "@/constants/routes/myPages" -import { getProfileSafely } from "@/lib/trpc/memoizedRequests" +import { auth } from "@/auth" import SignupForm from "@/components/Forms/Signup" import { getLang } from "@/i18n/serverContext" +import { isValidSession } from "@/utils/session" import type { SignupFormWrapperProps } from "@/types/components/blocks/dynamicContent" export default async function SignupFormWrapper({ dynamic_content, }: SignupFormWrapperProps) { - const user = await getProfileSafely() - if (user) { + const session = await auth() + if (isValidSession(session)) { // We don't want to allow users to access signup if they are already authenticated. redirect(overview[getLang()]) } diff --git a/components/Sidebar/JoinLoyalty/index.tsx b/components/Sidebar/JoinLoyalty/index.tsx index 8ee6bbb9d..d71c70c49 100644 --- a/components/Sidebar/JoinLoyalty/index.tsx +++ b/components/Sidebar/JoinLoyalty/index.tsx @@ -1,5 +1,6 @@ import { getName } from "@/lib/trpc/memoizedRequests" +import { auth } from "@/auth" import ArrowRight from "@/components/Icons/ArrowRight" import { ScandicFriends } from "@/components/Levels" import LoginButton from "@/components/LoginButton" @@ -8,6 +9,7 @@ import Link from "@/components/TempDesignSystem/Link" import Body from "@/components/TempDesignSystem/Text/Body" import Title from "@/components/TempDesignSystem/Text/Title" import { getIntl } from "@/i18n" +import { isValidSession } from "@/utils/session" import Contact from "./Contact" import ReadMore from "./ReadMore" @@ -19,10 +21,10 @@ import type { JoinLoyaltyContactProps } from "@/types/components/sidebar/joinLoy export default async function JoinLoyaltyContact({ block, }: JoinLoyaltyContactProps) { - const [intl, user] = await Promise.all([getIntl(), getName()]) + const [intl, session] = await Promise.all([getIntl(), auth()]) - // Check if we have user, that means we are logged in. - if (user) { + // Check if we valid session, that means we are logged in. + if (isValidSession(session)) { return null } diff --git a/components/Sidebar/MyPagesNavigation.tsx b/components/Sidebar/MyPagesNavigation.tsx index 722cbc7d7..c8d35adeb 100644 --- a/components/Sidebar/MyPagesNavigation.tsx +++ b/components/Sidebar/MyPagesNavigation.tsx @@ -1,16 +1,14 @@ import { Suspense } from "react" -import { getName } from "@/lib/trpc/memoizedRequests" - +import { auth } from "@/auth" import MyPagesSidebar from "@/components/MyPages/Sidebar" +import { isValidSession } from "@/utils/session" import SidebarNavigationSkeleton from "../MyPages/Sidebar/SidebarNavigationSkeleton" export default async function MyPagesNavigation() { - const user = await getName() - - // Check if we have user, that means we are logged in andt the My Pages menu can show. - if (!user) { + const session = await auth() + if (!isValidSession(session)) { return null } return ( diff --git a/utils/session.ts b/utils/session.ts new file mode 100644 index 000000000..3e00826ba --- /dev/null +++ b/utils/session.ts @@ -0,0 +1,18 @@ +import type { Session } from "next-auth" + +export function isValidSession(session: Session | null) { + if (!session) { + console.log("No session available (user not authenticated).") + return false + } + if (session.error) { + console.log(`Session error: ${session.error}`) + return false + } + if (session.token.expires_at && session.token?.expires_at < Date.now()) { + console.log(`Session expired: ${session.token?.expires_at}`) + return false + } + + return true +}