diff --git a/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx b/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx index c7d48640..315a7c71 100644 --- a/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx +++ b/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx @@ -4,10 +4,15 @@ import { isTokenExpired } from '@repo/mb-lib' import { cookies } from 'next/headers' import { Chat } from '@/components/routes/c/chat' import { getThread } from '@/services/hasura' -import { getUserProfile } from '@/services/supabase' +import { createSupabaseServerClient } from '@/services/supabase' export default async function ChatPage({ params }: ChatPageProps) { - const user = await getUserProfile() + const supabase = await createSupabaseServerClient() + const { + data: { user } + } = await supabase.auth.getUser() + if (!user || !user.email) throw new Error('user not found') + const jwt = cookies().get('hasuraJwt').value || '' console.log({ jwt, expired: isTokenExpired(jwt), user }) diff --git a/apps/masterbots.ai/app/c/[chatbot]/page.tsx b/apps/masterbots.ai/app/c/[chatbot]/page.tsx index 0141b9e1..066869b6 100644 --- a/apps/masterbots.ai/app/c/[chatbot]/page.tsx +++ b/apps/masterbots.ai/app/c/[chatbot]/page.tsx @@ -6,8 +6,8 @@ import { redirect } from 'next/navigation' import { ChatChatbot } from '@/components/routes/c/chat-chatbot' import ThreadPanel from '@/components/routes/c/thread-panel' import { botNames } from '@/lib/bots-names' -import { getChatbot, getThreads } from '@/services/hasura' -import { getUserProfile } from '@/services/supabase' +import { getChatbot, getThreads, getUser } from '@/services/hasura' +import { createSupabaseServerClient } from '@/services/supabase' export default async function BotThreadsPage({ params, @@ -16,7 +16,17 @@ export default async function BotThreadsPage({ params: { chatbot: string } searchParams: Record }) { - const user = await getUserProfile() + const supabase = await createSupabaseServerClient() + const { + data: { user } + } = await supabase.auth.getUser() + if (!user || !user.email) throw new Error('user not found') + const userProfile = await getUser({ + email: user.email, + adminSecret: process.env.HASURA_GRAPHQL_ADMIN_SECRET || '' + }) + + if (!userProfile) throw new Error('user not found') const jwt = cookies().get('hasuraJwt').value || '' // NOTE: maybe we should use same expiration time @@ -32,7 +42,7 @@ export default async function BotThreadsPage({ const threads = await getThreads({ chatbotName: botNames.get(params.chatbot), jwt, - userId: user.userId + userId: userProfile.userId }) // format all chatbot prompts as chatgpt 'system' messages diff --git a/apps/masterbots.ai/app/c/page.tsx b/apps/masterbots.ai/app/c/page.tsx index d0917350..c12276ac 100644 --- a/apps/masterbots.ai/app/c/page.tsx +++ b/apps/masterbots.ai/app/c/page.tsx @@ -3,11 +3,22 @@ import { redirect } from 'next/navigation' import { cookies } from 'next/headers' import ChatThreadListPanel from '@/components/routes/c/chat-thread-list-panel' import ThreadPanel from '@/components/routes/c/thread-panel' -import { getThreads } from '@/services/hasura' -import { getUserProfile } from '@/services/supabase' +import { getThreads, getUser } from '@/services/hasura' +import { createSupabaseServerClient } from '@/services/supabase' export default async function IndexPage() { - const user = await getUserProfile() + const supabase = await createSupabaseServerClient() + const { + data: { user } + } = await supabase.auth.getUser() + if (!user || !user.email) throw new Error('user not found') + const dbUserProfile = await getUser({ + email: user.email, + adminSecret: process.env.HASURA_GRAPHQL_ADMIN_SECRET || '' + }) + + if (!dbUserProfile) throw new Error('user not found') + const jwt = cookies().get('hasuraJwt').value || '' // NOTE: maybe we should use same expiration time @@ -15,7 +26,7 @@ export default async function IndexPage() { const threads = await getThreads({ jwt, - userId: user.userId + userId: dbUserProfile.userId }) return ( diff --git a/apps/masterbots.ai/components/layout/header.tsx b/apps/masterbots.ai/components/layout/header.tsx index 2ed83d03..a9f5ae07 100644 --- a/apps/masterbots.ai/components/layout/header.tsx +++ b/apps/masterbots.ai/components/layout/header.tsx @@ -5,13 +5,18 @@ import { cookies } from 'next/headers' import { Button } from '@/components/ui/button' import { IconSeparator } from '@/components/ui/icons' import { UserMenu } from '@/components/layout/user-menu' -import { getUserProfile } from '@/services/supabase' +import { createSupabaseServerClient } from '@/services/supabase' import { SidebarToggle } from '../routes/c/sidebar/sidebar-toggle' +import { getUser } from '@/services/hasura' // https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating export async function Header() { - const user = await getUserProfile() + const supabase = await createSupabaseServerClient() + const { + data: { user } + } = await supabase.auth.getUser() + if (!user || !user.email) throw new Error('user not found') const jwt = cookies().get('hasuraJwt')?.value || '' return ( diff --git a/apps/masterbots.ai/services/supabase/supa-browser-client.ts b/apps/masterbots.ai/services/supabase/client.ts similarity index 100% rename from apps/masterbots.ai/services/supabase/supa-browser-client.ts rename to apps/masterbots.ai/services/supabase/client.ts diff --git a/apps/masterbots.ai/services/supabase/index.ts b/apps/masterbots.ai/services/supabase/index.ts index b47b03a2..6b3426df 100644 --- a/apps/masterbots.ai/services/supabase/index.ts +++ b/apps/masterbots.ai/services/supabase/index.ts @@ -1,3 +1,2 @@ -export * from './supa-browser-client' +export * from './client' export * from './supa-server-client' -export * from './supa-server.service' diff --git a/apps/masterbots.ai/services/supabase/supa-server.service.ts b/apps/masterbots.ai/services/supabase/supa-server.service.ts deleted file mode 100644 index b154df97..00000000 --- a/apps/masterbots.ai/services/supabase/supa-server.service.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { UserProfile } from '@/hooks/use-global-store' -import { getUser } from '../hasura' -import { createSupabaseServerClient } from './supa-server-client' - -export async function getUserProfile(): Promise { - try { - return null - // const supabase = await createSupabaseServerClient() - // const { - // data: { user } - // } = await supabase.auth.getUser() - // if (!user || !user.email) throw new Error('user not found') - - // // TODO: use supabase - // const userProfile = await getUser({ - // email: user.email, - // adminSecret: process.env.HASURA_GRAPHQL_ADMIN_SECRET || '' - // }) - - // if (!userProfile) throw new Error('user not found') - // return { - // userId: userProfile.userId, - // username: userProfile.username, - // name: '', - // email: userProfile.email, - // image: userProfile.profilePicture || '' - // } - } catch (error) { - console.log('GET USER PROFILE ERROR', error) - return null - } -}