Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix permissions issue (#38799)

Jordi Enrice1ec2d2df0c0
diff --git a/apps/studio/hooks/misc/useCheckPermissions.ts b/apps/studio/hooks/misc/useCheckPermissions.ts
index 8b2106c3d58..96f3667c052 100644
--- a/apps/studio/hooks/misc/useCheckPermissions.ts
+++ b/apps/studio/hooks/misc/useCheckPermissions.ts
@@ -6,6 +6,7 @@ import { IS_PLATFORM } from 'lib/constants'
import type { Permission } from 'types'
import { useSelectedOrganizationQuery } from './useSelectedOrganization'
import { useSelectedProjectQuery } from './useSelectedProject'
+import { useMemo } from 'react'
const toRegexpString = (actionOrResource: string) =>
`^${actionOrResource.replace('.', '\\.').replace('%', '.*')}$`
@@ -156,33 +157,34 @@ export function useAsyncCheckPermissions(
isSuccess: isPermissionsSuccess,
} = useGetProjectPermissions(permissions, organizationSlug, projectRef, isLoggedIn)
- if (!isLoggedIn) {
- return {
- isLoading: true,
- isSuccess: false,
- can: false,
- }
- }
- if (!IS_PLATFORM) {
- return {
- isLoading: false,
- isSuccess: true,
- can: true,
- }
- }
-
- const can = doPermissionsCheck(
+ const can = useMemo(() => {
+ if (!IS_PLATFORM) return true
+ if (!isLoggedIn) return false
+ if (!isPermissionsSuccess || !allPermissions) return false
+
+ return doPermissionsCheck(
+ allPermissions,
+ action,
+ resource,
+ data,
+ _organizationSlug,
+ _projectRef
+ )
+ }, [
+ isLoggedIn,
+ isPermissionsSuccess,
allPermissions,
action,
resource,
data,
_organizationSlug,
- _projectRef
- )
+ _projectRef,
+ ])
- return {
- isLoading: isPermissionsLoading,
- isSuccess: isPermissionsSuccess,
- can,
- }
+ // Derive loading/success consistently from the same branches
+ const isLoading = !IS_PLATFORM ? false : !isLoggedIn ? true : isPermissionsLoading
+
+ const isSuccess = !IS_PLATFORM ? true : !isLoggedIn ? false : isPermissionsSuccess
+
+ return { isLoading, isSuccess, can }
}