Files
supabase/apps/studio/data/service-status/service-status-query.ts
supabase-vercel-tedd[bot] 248dbefbe9 fix(studio): skip notifications and service status queries without a session (#53538)
## Problem

`useNotificationsV2Query` and `useProjectServiceStatusQuery` run
whenever their component is mounted, whether or not a session exists.
When a session ends and the app redirects to `/sign-in`, they can still
fetch (or refetch) `/platform/notifications` and
`/v1/projects/{ref}/health` with no `Authorization` header. The API then
responds 401 `No access token provided`, and the response is classified
as an `UnclassifiedApiError` and reported.

## Solution

Both hooks now gate `enabled` on `useIsLoggedIn()` from `common`.
`usePermissionsQuery` and `useOverdueInvoicesQuery` already follow this
pattern. The caller's `enabled` option still applies on top of it.
Self-hosted builds always count as logged in, so their behavior is
unchanged.

Checks run:
- `pnpm turbo run typecheck --filter='...[origin/master]'`: passes
- `pnpm turbo run lint --filter='[origin/master]'`: passes. The first
run of `studio#lint:type-checks` ran out of heap memory. It passed on a
rerun with `NODE_OPTIONS=--max-old-space-size=8192`.
- `pnpm exec vitest run
components/layouts/AppLayout/AdvisorButton.test.tsx
components/layouts/AppLayout/AdvisorButton.platform.test.tsx
components/ui/AdvisorPanel/AdvisorSignals.integration.test.tsx` (in
`apps/studio`): passes
- Prettier check on the changed files: passes

## Review instructions

1. Sign out of the Studio preview. Open
`/dashboard/sign-in?returnTo=/project/<ref>`, keep the network tab open,
and confirm that no request to `/platform/notifications` or
`/v1/projects/<ref>/health` returns 401.
2. Sign in and open a project's home page. The service status indicator
still loads its status.
3. Open the Advisor/Issues panel from the header. Notifications still
load, and the header button still shows the unread/critical indicator
when it applies.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)


[FE-4989](https://linear.app/supabase/issue/FE-4989/unclassifiedapierror-no-access-token-provided)

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-11-18 08:51:47 -05:00

62 lines
1.9 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { components } from 'api-types'
import { useIsLoggedIn } from 'common'
import { serviceStatusKeys } from './keys'
import { get, handleError, unguarded } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomQueryOptions } from '@/types'
export type ProjectServiceStatusVariables = {
projectRef?: string
}
// Omit the 'healthy' field as it's equivalent to status = 'ACTIVE_HEALTHY'
export type ServiceHealthResponse = Omit<
components['schemas']['V1ServiceHealthResponse_Output'],
'healthy'
>
export type ProjectServiceStatus = ServiceHealthResponse['status']
export async function getProjectServiceStatus(
{ projectRef }: ProjectServiceStatusVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get(`/v1/projects/{ref}/health`, {
params: {
path: { ref: projectRef },
query: {
services: ['auth', 'realtime', 'rest', 'storage', 'db'],
},
},
signal,
grant: unguarded.migrating,
})
if (error) handleError(error)
return data as ServiceHealthResponse[]
}
export type ProjectServiceStatusData = Awaited<ReturnType<typeof getProjectServiceStatus>>
export type ProjectServiceStatusError = StudioError
export const useProjectServiceStatusQuery = <TData = ProjectServiceStatusData>(
{ projectRef }: ProjectServiceStatusVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<ProjectServiceStatusData, ProjectServiceStatusError, TData> = {}
) => {
const isLoggedIn = useIsLoggedIn()
return useQuery<ProjectServiceStatusData, ProjectServiceStatusError, TData>({
queryKey: serviceStatusKeys.serviceStatus(projectRef),
queryFn: ({ signal }) => getProjectServiceStatus({ projectRef }, signal),
enabled: enabled && isLoggedIn && typeof projectRef !== 'undefined',
...options,
})
}