Skip to content

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

1.8 KB
Raw Blame
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'

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

interface JWTSigningKeyUpdateVariables {
  projectRef?: string
  keyId: string
  status: 'in_use' | 'standby' | 'previously_used' | 'revoked'
}

export async function updateJWTSigningKey(payload: JWTSigningKeyUpdateVariables) {
  if (!payload.projectRef) throw new Error('projectRef is required')

  const { data, error } = await patch('/v1/projects/{ref}/config/auth/signing-keys/{id}', {
    params: {
      path: { ref: payload.projectRef, id: payload.keyId },
    },
    body: {
      status: payload.status,
    },
  })

  if (error) handleError(error)
  return data
}

type JWTSigningKeyUpdateData = Awaited<ReturnType<typeof updateJWTSigningKey>>

export const useJWTSigningKeyUpdateMutation = ({
  onSuccess,
  onError,
  ...options
}: Omit<
  UseCustomMutationOptions<JWTSigningKeyUpdateData, ResponseError, JWTSigningKeyUpdateVariables>,
  'mutationFn'
> = {}) => {
  const queryClient = useQueryClient()

  return useMutation<JWTSigningKeyUpdateData, ResponseError, JWTSigningKeyUpdateVariables>({
    mutationFn: (vars) => updateJWTSigningKey(vars),
    async onSuccess(data, variables, context) {
      const { projectRef } = variables

      await queryClient.invalidateQueries({ queryKey: jwtSigningKeysKeys.list(projectRef) })

      await onSuccess?.(data, variables, context)
    },
    async onError(data, variables, context) {
      if (onError === undefined) {
        toast.error(`Failed to update new JWT signing key: ${data.message}`)
      } else {
        onError(data, variables, context)
      }
    },
    ...options,
  })
}