Skip to content

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

Ensure that permissions are fetched properly for a new user, and avoid making unnecessary call to fetch permissions if profile is not available

Joshen Limbbafed9e1250
diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsx
index 3ced511a8f2..c420febda47 100644
--- a/studio/hooks/misc/withAuth.tsx
+++ b/studio/hooks/misc/withAuth.tsx
@@ -27,7 +27,11 @@ export function withAuth<T>(
const returning =
app.projects.isInitialized && app.organizations.isInitialized ? 'minimal' : undefined
const { profile, isLoading } = useProfile(returning)
- const { permissions, isLoading: isPermissionLoading } = usePermissions(returning)
+ const {
+ permissions,
+ isLoading: isPermissionLoading,
+ mutate: mutatePermissions,
+ } = usePermissions(profile, returning)
const isAccessingBlockedPage = !IS_PLATFORM && PLATFORM_ONLY_PAGES.includes(page)
const isRedirecting =
@@ -42,12 +46,9 @@ export function withAuth<T>(
} else if (returning !== 'minimal') {
ui.setProfile(profile)
- if (!app.organizations.isInitialized) {
- app.organizations.load()
- }
- if (!app.projects.isInitialized) {
- app.projects.load()
- }
+ if (!app.organizations.isInitialized) app.organizations.load()
+ if (!app.projects.isInitialized) app.projects.load()
+ mutatePermissions()
}
}
diff --git a/studio/hooks/queries/usePermissions.ts b/studio/hooks/queries/usePermissions.ts
index 8bf6c42c7e0..f0045919822 100644
--- a/studio/hooks/queries/usePermissions.ts
+++ b/studio/hooks/queries/usePermissions.ts
@@ -4,7 +4,7 @@ import { get } from 'lib/common/fetch'
import { API_URL, IS_PLATFORM } from 'lib/constants'
import useSWR from 'swr'
-export function usePermissions(returning?: 'minimal') {
+export function usePermissions(profile?: any, returning?: 'minimal') {
let url = `${API_URL}/profile/permissions`
if (returning) {
@@ -12,13 +12,18 @@ export function usePermissions(returning?: 'minimal') {
url = `${url}?${query}`
}
- const { data: data, error } = useSWR<any>(url, get, { loadingTimeout: 10000 })
+ const {
+ data: data,
+ error,
+ mutate,
+ } = useSWR<any>(profile !== undefined ? url : null, get, { loadingTimeout: 10000 })
const anyError = data?.error || error
return {
permissions: anyError ? undefined : data,
isLoading: !anyError && !data,
isError: !!anyError,
+ mutate,
}
}
diff --git a/studio/hooks/queries/useProfile.ts b/studio/hooks/queries/useProfile.ts
index 3d5802391f8..33a5aa29581 100644
--- a/studio/hooks/queries/useProfile.ts
+++ b/studio/hooks/queries/useProfile.ts
@@ -11,9 +11,7 @@ export function useProfile(returning?: 'minimal') {
url = `${url}?${query}`
}
- const { data, error } = useSWR<any>(url, get, {
- loadingTimeout: 10000,
- })
+ const { data, error } = useSWR<any>(url, get, { loadingTimeout: 10000 })
const anyError = data?.error || error
function mutateProfile(updatedUser: User, revalidate?: boolean) {
diff --git a/studio/stores/app/PermissionsStore.ts b/studio/stores/app/PermissionsStore.ts
new file mode 100644
index 00000000000..a14f98cc1d0
--- /dev/null
+++ b/studio/stores/app/PermissionsStore.ts
@@ -0,0 +1,36 @@
+import { keyBy } from 'lodash'
+import { Permission } from 'types'
+import { IRootStore } from '../RootStore'
+import PostgresMetaInterface from '../common/PostgresMetaInterface'
+
+export default class PermissionsStore extends PostgresMetaInterface<Permission> {
+ constructor(
+ rootStore: IRootStore,
+ dataUrl: string,
+ headers?: {
+ [prop: string]: any
+ },
+ options?: { identifier: string }
+ ) {
+ super(rootStore, dataUrl, headers, options)
+ }
+
+ // [Joshen] Method override here as permissions are not identified by an id from the API
+ setDataArray(value: any[]) {
+ const formattedValues = value.map((permission, idx: number) => {
+ return { ...permission, id: idx + 1 }
+ })
+ const formattedValue = keyBy(formattedValues, this.identifier)
+ Object.keys(this.data).forEach((projectId) => {
+ if (formattedValue[projectId] !== undefined) {
+ formattedValue[projectId] = this.data[projectId]
+ }
+ })
+ this.data = formattedValue
+ }
+
+ // [Joshen] Method override here as permissions do not have `name` property
+ list() {
+ return Object.values(this.data)
+ }
+}