Skip to content

Commit

Permalink
fix: chat auth bug (#109)
Browse files Browse the repository at this point in the history
* fix: chat auth bug

* fix: chat auth bug

* fix: chat auth bug
  • Loading branch information
gaboesquivel authored Apr 8, 2024
1 parent ffe0280 commit 1d1166d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
20 changes: 2 additions & 18 deletions apps/masterbots.ai/app/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 },
Expand Down
7 changes: 4 additions & 3 deletions apps/masterbots.ai/app/c/[chatbot]/[threadId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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?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
})
Expand Down
7 changes: 4 additions & 3 deletions apps/masterbots.ai/app/c/[chatbot]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ 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?next=/${params.chatbot}`)
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?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
Expand Down
5 changes: 3 additions & 2 deletions apps/masterbots.ai/app/c/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ''

Expand Down

0 comments on commit 1d1166d

Please sign in to comment.