From b5131ce125160d8a1e1c2561d7cd323cad476b51 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Wed, 28 May 2025 16:30:25 -0600 Subject: [PATCH] fix query parser --- web/src/app/api/currency/route.ts | 4 +- web/src/app/embed/layout.tsx | 2 +- web/src/lib/query.ts | 3 +- .../lib/supabase/getSupabaseUserContext.ts | 71 ++++++++----------- 4 files changed, 36 insertions(+), 44 deletions(-) diff --git a/web/src/app/api/currency/route.ts b/web/src/app/api/currency/route.ts index d04a21afd..6f23d3ec7 100644 --- a/web/src/app/api/currency/route.ts +++ b/web/src/app/api/currency/route.ts @@ -10,9 +10,9 @@ export async function GET(request: NextRequest) { if (user && !user?.is_anonymous) { return NextResponse.json(currencies); - } else { - return NextResponse.error(); } + + return NextResponse.error(); } const currencies = [ diff --git a/web/src/app/embed/layout.tsx b/web/src/app/embed/layout.tsx index fff638121..4a247e2c6 100644 --- a/web/src/app/embed/layout.tsx +++ b/web/src/app/embed/layout.tsx @@ -13,7 +13,7 @@ export default async function EmbedLayout({ children }: { children: React.ReactN
- <>{children} + {children}
diff --git a/web/src/lib/query.ts b/web/src/lib/query.ts index 73cca7db8..d57c4e019 100644 --- a/web/src/lib/query.ts +++ b/web/src/lib/query.ts @@ -3,7 +3,8 @@ import { userQueryKeys } from '@/api/query_keys/users'; import type { QueryClient, queryOptions } from '@tanstack/react-query'; export const isQueryStale = ( - options: ReturnType>, + // biome-ignore lint/suspicious/noExplicitAny: It really doesn't matter what the type is here + options: ReturnType>, queryClient: QueryClient ): boolean => { const queryState = queryClient.getQueryState(options.queryKey); diff --git a/web/src/lib/supabase/getSupabaseUserContext.ts b/web/src/lib/supabase/getSupabaseUserContext.ts index 07a946983..ac36749fe 100644 --- a/web/src/lib/supabase/getSupabaseUserContext.ts +++ b/web/src/lib/supabase/getSupabaseUserContext.ts @@ -1,57 +1,48 @@ 'use server'; -import type { Session, User } from '@supabase/supabase-js'; -import type { Database } from './database.types'; -import type { createClient } from './server'; +import { createClient } from './server'; import { signInWithAnonymousUser } from './signIn'; type PromiseType> = T extends Promise ? U : never; export type UseSupabaseUserContextType = PromiseType>; -export const getSupabaseUserContext = async (supabase: ReturnType) => { - let userContext: { user: User | null; session: Session | null } = { - user: null, - session: null - }; +export const getSupabaseUserContext = async (preemptiveRefreshMinutes = 5) => { + const supabase = await createClient(); - try { - let sessionData = await supabase.auth.getSession(); + // Get the session first + let { data: sessionData } = await supabase.auth.getSession(); - // If no session exists, try to refresh - if (!sessionData.session) { - const refreshedSessionData = await supabase.auth.refreshSession(); + // Check if we need to refresh the session + if (sessionData.session) { + const refreshedSessionData = (await refreshSessionIfNeeded( + supabase, + sessionData.session, + preemptiveRefreshMinutes + )) as Awaited>; - // If session was refreshed, get the updated session - if (refreshedSessionData?.session) { - // Replace the entire sessionData object to avoid type issues - sessionData = refreshedSessionData; - } + // If session was refreshed, get the updated session + if (refreshedSessionData && 'session' in refreshedSessionData) { + // Replace the entire sessionData object to avoid type issues + sessionData = refreshedSessionData; } - - // If we still don't have a session, sign in anonymously - if (!sessionData.session) { - const anonymousSignInResult = await signInWithAnonymousUser(supabase); - if (anonymousSignInResult.data?.session) { - sessionData = { - data: { - session: anonymousSignInResult.data.session, - user: anonymousSignInResult.data.user - }, - error: null - }; - } - } - - userContext = { - user: sessionData.data.session?.user || null, - session: sessionData.data.session || null - }; - } catch (error) { - console.error('Error getting user context:', error); } - return userContext; + // Get user data + const { data: userData } = await supabase.auth.getUser(); + + if (!userData.user) { + const { session: anonSession } = await signInWithAnonymousUser(); + return { + user: anonSession?.user || null, + accessToken: anonSession?.access_token + }; + } + + const user = userData.user; + const accessToken = sessionData.session?.access_token; + const refreshToken = sessionData.session?.refresh_token; + return { user, accessToken, refreshToken }; }; /**