From 352ec874c1d1b65962effe9b3bff0e6ffd9880a2 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Wed, 3 Apr 2024 16:13:06 +0800 Subject: [PATCH] Refactor getUser function in marketing layout The code refactors the getUser function in the marketing layout file within the web app. It simplifies code by directly retrieving user information within the function and eliminates unnecessary use of Supabase server component client. This change improves efficiency and readability, and reduces potential errors by restricting server pings to necessary operations only. --- apps/web/app/(marketing)/layout.tsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/apps/web/app/(marketing)/layout.tsx b/apps/web/app/(marketing)/layout.tsx index c6916d2d3..260c7e727 100644 --- a/apps/web/app/(marketing)/layout.tsx +++ b/apps/web/app/(marketing)/layout.tsx @@ -5,11 +5,7 @@ import { SiteHeader } from '~/(marketing)/_components/site-header'; import { withI18n } from '~/lib/i18n/with-i18n'; async function SiteLayout(props: React.PropsWithChildren) { - const client = getSupabaseServerComponentClient(); - - const { - data: { user }, - } = await client.auth.getUser(); + const user = await getUser(); return ( <> @@ -23,3 +19,18 @@ async function SiteLayout(props: React.PropsWithChildren) { } export default withI18n(SiteLayout); + +async function getUser() { + const client = getSupabaseServerComponentClient(); + + // Supabase is going to be complaining about this line + // since we use getSession instead of getUser + // we don't quite care because we only need to know if the user is logged in + // to display the user menu in the header. + // There is no need to ping the server while navigating to marketing pages. + const { + data: { session }, + } = await client.auth.getSession(); + + return session?.user; +}