Skip to content

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

1.3 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'

import { jwtSigningKeysKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

interface LegacyJWTSigningKeyVariables {
  projectRef?: string
}

async function getLegacyJWTSigningKey(
  { projectRef }: LegacyJWTSigningKeyVariables,
  signal?: AbortSignal
) {
  if (!projectRef) throw new Error('projectRef is required')

  const { data, error } = await get(`/v1/projects/{ref}/config/auth/signing-keys/legacy`, {
    params: { path: { ref: projectRef } },
    signal,
  })

  if (error) {
    handleError(error)
  }

  return data
}

export type LegacyJWTSigningKeyData = Awaited<ReturnType<typeof getLegacyJWTSigningKey>>

export const useLegacyJWTSigningKeyQuery = <TData = LegacyJWTSigningKeyData>(
  { projectRef }: LegacyJWTSigningKeyVariables,
  { enabled, ...options }: UseCustomQueryOptions<LegacyJWTSigningKeyData, ResponseError, TData> = {}
) =>
  useQuery<LegacyJWTSigningKeyData, ResponseError, TData>({
    queryKey: jwtSigningKeysKeys.legacy(projectRef),
    queryFn: ({ signal }) => getLegacyJWTSigningKey({ projectRef }, signal),
    enabled: enabled && !!projectRef,
    retry: false,
    refetchOnWindowFocus: false,
    ...options,
  })