Skip to content

Commit

Permalink
fix: use 'serverInfo' to check whether chat is enabled, avoid using a…
Browse files Browse the repository at this point in the history
…dmin access query 'workers' (#1477)
  • Loading branch information
wsxiaoys authored Feb 18, 2024
1 parent 5c4baec commit e621a5a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
7 changes: 2 additions & 5 deletions ee/tabby-ui/components/user-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react'
import { has } from 'lodash-es'
import NiceAvatar, { genConfig } from 'react-nice-avatar'

import { WorkerKind } from '@/lib/gql/generates/graphql'
import { useWorkers } from '@/lib/hooks/use-workers'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { useAuthenticatedSession, useSignOut } from '@/lib/tabby/auth'
import {
DropdownMenu,
Expand All @@ -20,8 +18,7 @@ export default function UserPanel() {
const user = useAuthenticatedSession()
const signOut = useSignOut()

const workers = useWorkers()
const isChatEnabled = has(workers, WorkerKind.Chat)
const isChatEnabled = useIsChatEnabled()

if (!user) {
return
Expand Down
32 changes: 32 additions & 0 deletions ee/tabby-ui/lib/hooks/use-server-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useQuery } from 'urql'

import { graphql } from '@/lib/gql/generates'

export const getServerInfo = graphql(/* GraphQL */ `
query GetServerInfo {
serverInfo {
isAdminInitialized
isEmailConfigured
isChatEnabled
}
}
`)

const useServerInfo = () => {
const [{ data }] = useQuery({ query: getServerInfo })
return data?.serverInfo
}

const useIsChatEnabled = () => {
return useServerInfo()?.isChatEnabled
}

const useIsAdminInitialized = () => {
return useServerInfo()?.isAdminInitialized
}

const useIsEmailConfigured = () => {
return useServerInfo()?.isEmailConfigured
}

export { useIsChatEnabled, useIsAdminInitialized, useIsEmailConfigured }
16 changes: 6 additions & 10 deletions ee/tabby-ui/lib/tabby/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as React from 'react'
import { usePathname, useRouter } from 'next/navigation'
import { jwtDecode, JwtPayload } from 'jwt-decode'
import { useQuery } from 'urql'
import useLocalStorage from 'use-local-storage'

import { graphql } from '@/lib/gql/generates'
import { isClientSide } from '@/lib/utils'

import { useIsAdminInitialized } from '../hooks/use-server-info'

interface AuthData {
accessToken: string
refreshToken: string
Expand Down Expand Up @@ -279,30 +280,25 @@ function useSession(): Session {
return session
}

export const getIsAdminInitialized = graphql(/* GraphQL */ `
query GetIsAdminInitialized {
isAdminInitialized
}
`)

const redirectWhitelist = ['/auth/signin', '/auth/signup']

function useAuthenticatedSession() {
const [{ data }] = useQuery({ query: getIsAdminInitialized })
const isAdminInitialized = useIsAdminInitialized()
const router = useRouter()
const pathName = usePathname()
const { data: session, status } = useSession()

React.useEffect(() => {
if (status === 'loading') return
if (status === 'authenticated') return
if (isAdminInitialized === undefined) return

if (data?.isAdminInitialized === false) {
if (!isAdminInitialized) {
router.replace('/auth/signup?isAdmin=true')
} else if (!redirectWhitelist.includes(pathName)) {
router.replace('/auth/signin')
}
}, [data, status])
}, [isAdminInitialized, status])

return session
}
Expand Down

0 comments on commit e621a5a

Please sign in to comment.