From f1278895489b6a1e4f145abf522172368f333377 Mon Sep 17 00:00:00 2001 From: Gabo Esquivel Date: Mon, 8 Apr 2024 14:42:45 -0600 Subject: [PATCH 1/3] fix: chat auth bug --- apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx | 2 +- apps/masterbots.ai/app/c/[chatbot]/page.tsx | 4 ++-- apps/masterbots.ai/app/c/page.tsx | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx b/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx index 78de3fa1..edfbef2b 100644 --- a/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx +++ b/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx @@ -11,7 +11,7 @@ export default async function ChatPage({ params }: ChatPageProps) { const { data: { user } } = await supabase.auth.getUser() - if (!user || !user.email) throw new Error('user not found') + if (!user || !user.email) redirect(`/auth/sign-in`) const jwt = cookies().get('hasuraJwt')?.value || '' diff --git a/apps/masterbots.ai/app/c/[chatbot]/page.tsx b/apps/masterbots.ai/app/c/[chatbot]/page.tsx index 587feaa9..77976bf7 100644 --- a/apps/masterbots.ai/app/c/[chatbot]/page.tsx +++ b/apps/masterbots.ai/app/c/[chatbot]/page.tsx @@ -20,13 +20,13 @@ export default async function BotThreadsPage({ const { data: { user } } = await supabase.auth.getUser() - if (!user || !user.email) throw new Error('user not found') + if (!user || !user.email) redirect(`/auth/sign-in`) const userProfile = await getUser({ email: user.email, adminSecret: process.env.HASURA_GRAPHQL_ADMIN_SECRET || '' }) - if (!userProfile) throw new Error('user not found') + if (!userProfile) redirect(`/auth/sign-in`) const jwt = cookies().get('hasuraJwt')?.value || '' // NOTE: maybe we should use same expiration time diff --git a/apps/masterbots.ai/app/c/page.tsx b/apps/masterbots.ai/app/c/page.tsx index c12276ac..848a6531 100644 --- a/apps/masterbots.ai/app/c/page.tsx +++ b/apps/masterbots.ai/app/c/page.tsx @@ -11,13 +11,14 @@ export default async function IndexPage() { const { data: { user } } = await supabase.auth.getUser() - if (!user || !user.email) throw new Error('user not found') + if (!user || !user.email) redirect(`/auth/sign-in`) + const dbUserProfile = await getUser({ email: user.email, adminSecret: process.env.HASURA_GRAPHQL_ADMIN_SECRET || '' }) - if (!dbUserProfile) throw new Error('user not found') + if (!dbUserProfile) redirect(`/auth/sign-in`) const jwt = cookies().get('hasuraJwt').value || '' From 7caa6e2e3bfa1c198e496470580ce7f05e560326 Mon Sep 17 00:00:00 2001 From: Gabo Esquivel Date: Mon, 8 Apr 2024 14:47:28 -0600 Subject: [PATCH 2/3] fix: chat auth bug --- apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx | 7 ++++--- apps/masterbots.ai/app/c/[chatbot]/page.tsx | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx b/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx index edfbef2b..5547850a 100644 --- a/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx +++ b/apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx @@ -11,14 +11,15 @@ export default async function ChatPage({ params }: ChatPageProps) { const { data: { user } } = await supabase.auth.getUser() - if (!user || !user.email) redirect(`/auth/sign-in`) - + if (!user || !user.email) + redirect(`/auth/sign-in?next=/${params.chatbot}/${params.threadId}`) const jwt = cookies().get('hasuraJwt')?.value || '' console.log({ jwt, expired: isTokenExpired(jwt), user }) // NOTE: maybe we should use same expiration time if (!jwt || isTokenExpired(jwt) || !user) - redirect(`/auth/sign-in?next=/${params.threadId}/${params.threadId}`) + redirect(`/auth/sign-in?next=/${params.chatbot}/${params.threadId}`) + const thread = await getThread({ threadId: params.threadId }) diff --git a/apps/masterbots.ai/app/c/[chatbot]/page.tsx b/apps/masterbots.ai/app/c/[chatbot]/page.tsx index 77976bf7..b358e12c 100644 --- a/apps/masterbots.ai/app/c/[chatbot]/page.tsx +++ b/apps/masterbots.ai/app/c/[chatbot]/page.tsx @@ -20,17 +20,18 @@ export default async function BotThreadsPage({ const { data: { user } } = await supabase.auth.getUser() - if (!user || !user.email) redirect(`/auth/sign-in`) + if (!user || !user.email) redirect(`/auth/sign-in?next=/${params.chatbot}`) const userProfile = await getUser({ email: user.email, adminSecret: process.env.HASURA_GRAPHQL_ADMIN_SECRET || '' }) - if (!userProfile) redirect(`/auth/sign-in`) + if (!userProfile) redirect(`/auth/sign-in?next=/${params.chatbot}`) const jwt = cookies().get('hasuraJwt')?.value || '' // NOTE: maybe we should use same expiration time - if (!jwt || isTokenExpired(jwt) || !user) redirect(`/auth/sign-in?next=/c`) + if (!jwt || isTokenExpired(jwt)) + redirect(`/auth/sign-in?next=/${params.chatbot}`) const chatbot = await getChatbot({ chatbotName: botNames.get(params.chatbot), jwt From 5028b53b54e9caa5253cb360f0f4535cc0deb845 Mon Sep 17 00:00:00 2001 From: Gabo Esquivel Date: Mon, 8 Apr 2024 14:51:56 -0600 Subject: [PATCH 3/3] fix: chat auth bug --- apps/masterbots.ai/app/auth/callback/route.ts | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/apps/masterbots.ai/app/auth/callback/route.ts b/apps/masterbots.ai/app/auth/callback/route.ts index 657377ba..dbc040e1 100644 --- a/apps/masterbots.ai/app/auth/callback/route.ts +++ b/apps/masterbots.ai/app/auth/callback/route.ts @@ -4,6 +4,7 @@ import { type CookieOptions, createServerClient } from '@supabase/ssr' import { getToken, validateJwtSecret } from '@repo/mb-lib' import { upsertUser } from '@/services/hasura' import { nanoid } from '@/lib/utils' +import { createSupabaseServerClient } from '@/services/supabase' export async function GET(request: Request) { const { searchParams, origin } = new URL(request.url) @@ -12,24 +13,7 @@ export async function GET(request: Request) { // if "next" is in param, use it as the redirect URL const next = searchParams.get('next') ?? '/' - const cookieStore = cookies() - const supabase = createServerClient( - process.env.NEXT_PUBLIC_SUPABASE_URL, - process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, - { - cookies: { - get(name: string) { - return cookieStore.get(name).value - }, - set(name: string, value: string, options: CookieOptions) { - cookieStore.set({ name, value, ...options }) - }, - remove(name: string, options: CookieOptions) { - cookieStore.delete({ name, ...options }) - } - } - } - ) + const supabase = await createSupabaseServerClient() const { data: { user },