Skip to content

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

Skip unnecessary calls if user is not logged in

Joshen Lim0c73213dc740
diff --git a/studio/components/interfaces/App/RouteValidationWrapper.tsx b/studio/components/interfaces/App/RouteValidationWrapper.tsx
index 3b82a9b1679..419f458f7a7 100644
--- a/studio/components/interfaces/App/RouteValidationWrapper.tsx
+++ b/studio/components/interfaces/App/RouteValidationWrapper.tsx
@@ -2,7 +2,7 @@ import { observer } from 'mobx-react-lite'
import { useRouter } from 'next/router'
import { PropsWithChildren, useEffect } from 'react'
-import { useParams } from 'common'
+import { useAuth, useParams } from 'common'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useProjectsQuery } from 'data/projects/projects-query'
import { useFlag, useStore } from 'hooks'
@@ -16,6 +16,9 @@ const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
const { ref, slug } = useParams()
const navLayoutV2 = useFlag('navigationLayoutV2')
+ const { session } = useAuth()
+ const isLoggedIn = Boolean(session)
+
/**
* Array of urls/routes that should be ignored
*/
@@ -37,12 +40,14 @@ const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
return excemptUrls.includes(router?.pathname)
}
- const { data: organizations, isSuccess: orgsInitialized } = useOrganizationsQuery()
+ const { data: organizations, isSuccess: orgsInitialized } = useOrganizationsQuery({
+ enabled: isLoggedIn,
+ })
const organizationsRef = useLatest(organizations)
useEffect(() => {
// check if current route is excempted from route validation check
- if (isExceptUrl()) return
+ if (isExceptUrl() || !isLoggedIn) return
if (orgsInitialized && slug) {
// Check validity of organization that user is trying to access
@@ -57,12 +62,14 @@ const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
}
}, [orgsInitialized])
- const { data: projects, isSuccess: projectsInitialized } = useProjectsQuery()
+ const { data: projects, isSuccess: projectsInitialized } = useProjectsQuery({
+ enabled: isLoggedIn,
+ })
const projectsRef = useLatest(projects)
useEffect(() => {
// check if current route is excempted from route validation check
- if (isExceptUrl()) return
+ if (isExceptUrl() || !isLoggedIn) return
if (projectsInitialized && ref) {
// Check validity of project that the user is trying to access
diff --git a/studio/components/ui/PageTelemetry.tsx b/studio/components/ui/PageTelemetry.tsx
index ddba717617f..0b4cfafae94 100644
--- a/studio/components/ui/PageTelemetry.tsx
+++ b/studio/components/ui/PageTelemetry.tsx
@@ -1,4 +1,4 @@
-import { useParams, useTelemetryProps } from 'common'
+import { useAuth, useParams, useTelemetryProps } from 'common'
import { observer } from 'mobx-react-lite'
import { useRouter } from 'next/router'
import { PropsWithChildren, useEffect } from 'react'
@@ -10,9 +10,12 @@ import { API_URL, IS_PLATFORM } from 'lib/constants'
const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {
const router = useRouter()
const { ref } = useParams()
+ const { session } = useAuth()
const telemetryProps = useTelemetryProps()
const selectedOrganization = useSelectedOrganization()
+ const isLoggedIn = Boolean(session)
+
useEffect(() => {
function handleRouteChange(url: string) {
handlePageTelemetry(url)
@@ -59,14 +62,16 @@ const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {
},
})
- post(`${API_URL}/telemetry/pageview`, {
- ...(ref && { projectRef: ref }),
- ...(selectedOrganization && { orgSlug: selectedOrganization.slug }),
- referrer: referrer,
- title: document.title,
- path: router.route,
- location: router.asPath,
- })
+ if (isLoggedIn) {
+ post(`${API_URL}/telemetry/pageview`, {
+ ...(ref && { projectRef: ref }),
+ ...(selectedOrganization && { orgSlug: selectedOrganization.slug }),
+ referrer: referrer,
+ title: document.title,
+ path: router.route,
+ location: router.asPath,
+ })
+ }
}
}
diff --git a/studio/data/permissions/permissions-query.ts b/studio/data/permissions/permissions-query.ts
index b96a57c6698..d98a9a2a15b 100644
--- a/studio/data/permissions/permissions-query.ts
+++ b/studio/data/permissions/permissions-query.ts
@@ -1,7 +1,8 @@
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
+import { useCallback } from 'react'
+
import { get } from 'lib/common/fetch'
import { API_URL, IS_PLATFORM } from 'lib/constants'
-import { useCallback } from 'react'
import { Permission, ResponseError } from 'types'
import { permissionKeys } from './keys'
diff --git a/studio/data/profile/profile-query.ts b/studio/data/profile/profile-query.ts
index a11e5050e16..53ab747ba7f 100644
--- a/studio/data/profile/profile-query.ts
+++ b/studio/data/profile/profile-query.ts
@@ -1,10 +1,10 @@
-import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+
import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
-import { useCallback } from 'react'
+import { ResponseError } from 'types'
import { profileKeys } from './keys'
import { Profile } from './types'
-import { ResponseError } from 'types'
export type ProfileResponse = Profile
@@ -30,14 +30,7 @@ export const useProfileQuery = <TData = ProfileData>({
{
staleTime: 1000 * 60 * 30, // default good for 30 mins
...options,
+ enabled,
}
)
}
-
-export const useProfilePrefetch = () => {
- const client = useQueryClient()
-
- return useCallback(() => {
- client.prefetchQuery(profileKeys.profile(), ({ signal }) => getProfile(signal))
- }, [])
-}
diff --git a/studio/hooks/misc/useCheckPermissions.ts b/studio/hooks/misc/useCheckPermissions.ts
index f1228eda934..3eb0fc24880 100644
--- a/studio/hooks/misc/useCheckPermissions.ts
+++ b/studio/hooks/misc/useCheckPermissions.ts
@@ -4,6 +4,7 @@ import { usePermissionsQuery } from 'data/permissions/permissions-query'
import { IS_PLATFORM } from 'lib/constants'
import { Permission } from 'types'
import { useSelectedOrganization } from './useSelectedOrganization'
+import { useAuth } from 'common'
const toRegexpString = (actionOrResource: string) =>
`^${actionOrResource.replace('.', '\\.').replace('%', '.*')}$`
@@ -67,11 +68,16 @@ export function useCheckPermissions(
organizationId?: number,
permissions?: Permission[]
) {
+ const { session } = useAuth()
+ const isLoggedIn = Boolean(session)
+
const { permissions: allPermissions, organizationId: orgId } = useGetPermissions(
permissions,
- organizationId
+ organizationId,
+ isLoggedIn
)
+ if (!isLoggedIn) return false
if (!IS_PLATFORM) return true
return doPermissionsCheck(allPermissions, action, resource, data, orgId)
diff --git a/studio/hooks/misc/useSelectedOrganization.ts b/studio/hooks/misc/useSelectedOrganization.ts
index bc6be1b3710..baf66658768 100644
--- a/studio/hooks/misc/useSelectedOrganization.ts
+++ b/studio/hooks/misc/useSelectedOrganization.ts
@@ -1,13 +1,16 @@
-import { useParams } from 'common'
+import { useAuth, useParams } from 'common'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useMemo } from 'react'
-import { useSelectedProject } from './useSelectedProject'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
+import { useSelectedProject } from './useSelectedProject'
export function useSelectedOrganization({ enabled = true } = {}) {
+ const { session } = useAuth()
+ const isLoggedIn = Boolean(session)
+
const { slug } = useParams()
- const { data } = useOrganizationsQuery({ enabled })
+ const { data } = useOrganizationsQuery({ enabled: isLoggedIn && enabled })
const selectedProject = useSelectedProject()
const localStorageSlug = useMemo(() => {
diff --git a/studio/lib/auth.tsx b/studio/lib/auth.tsx
index a0e3e21ed7a..12fd3097c06 100644
--- a/studio/lib/auth.tsx
+++ b/studio/lib/auth.tsx
@@ -16,7 +16,6 @@ import { clearLocalStorage, resetSignInClicks } from './local-storage'
export const AuthContext = AuthContextInternal
export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
- const queryClient = useQueryClient()
const { ui } = useStore()
const router = useRouter()
const telemetryProps = useTelemetryProps()
diff --git a/studio/lib/profile.tsx b/studio/lib/profile.tsx
index d8feec8dcbc..8b184598b2c 100644
--- a/studio/lib/profile.tsx
+++ b/studio/lib/profile.tsx
@@ -1,8 +1,8 @@
import { useQueryClient } from '@tanstack/react-query'
+import { useAuth, useTelemetryProps } from 'common'
import { useRouter } from 'next/router'
import { PropsWithChildren, createContext, useContext, useMemo } from 'react'
-import { useTelemetryProps } from 'common'
import { invalidateOrganizationsQuery } from 'data/organizations/organizations-query'
import { usePermissionsQuery } from 'data/permissions/permissions-query'
import { useProfileCreateMutation } from 'data/profile/profile-create-mutation'
@@ -29,11 +29,14 @@ export const ProfileContext = createContext<ProfileContextType>({
})
export const ProfileProvider = ({ children }: PropsWithChildren<{}>) => {
- const queryClient = useQueryClient()
const { ui } = useStore()
const router = useRouter()
+ const queryClient = useQueryClient()
const telemetryProps = useTelemetryProps()
+ const { session } = useAuth()
+ const isLoggedIn = Boolean(session)
+
const { mutate: createProfile, isLoading: isCreatingProfile } = useProfileCreateMutation({
async onSuccess() {
Telemetry.sendEvent(
@@ -60,6 +63,7 @@ export const ProfileProvider = ({ children }: PropsWithChildren<{}>) => {
isError,
isSuccess,
} = useProfileQuery({
+ enabled: isLoggedIn,
onSuccess(profile) {
Telemetry.sendIdentify(profile, telemetryProps)
},
@@ -71,7 +75,7 @@ export const ProfileProvider = ({ children }: PropsWithChildren<{}>) => {
},
})
- const { isInitialLoading: isLoadingPermissions } = usePermissionsQuery()
+ const { isInitialLoading: isLoadingPermissions } = usePermissionsQuery({ enabled: isLoggedIn })
const value = useMemo(() => {
const isLoading = isLoadingProfile || isCreatingProfile || isLoadingPermissions