Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: supabase ssr bug #106

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
18 changes: 14 additions & 4 deletions apps/masterbots.ai/app/c/[chatbot]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,7 +16,17 @@ export default async function BotThreadsPage({
params: { chatbot: string }
searchParams: Record<string, string | string[] | undefined>
}) {
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
Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions apps/masterbots.ai/app/c/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@ 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
if (!jwt || isTokenExpired(jwt) || !user) redirect(`/auth/sign-in`)

const threads = await getThreads({
jwt,
userId: user.userId
userId: dbUserProfile.userId
})

return (
Expand Down
9 changes: 7 additions & 2 deletions apps/masterbots.ai/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 1 addition & 2 deletions apps/masterbots.ai/services/supabase/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './supa-browser-client'
export * from './client'
export * from './supa-server-client'
export * from './supa-server.service'
32 changes: 0 additions & 32 deletions apps/masterbots.ai/services/supabase/supa-server.service.ts

This file was deleted.

Loading