mirror of
https://github.com/supabase/supabase.git
synced 2026-05-12 04:16:08 +08:00
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { useCallback } from 'react'
|
|
|
|
import { authKeys } from './keys'
|
|
import type { components } from '@/data/api'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type AuthConfigVariables = {
|
|
projectRef?: string
|
|
}
|
|
|
|
export type AuthConfigResponse = components['schemas']['GoTrueConfigResponse']
|
|
|
|
export async function getProjectAuthConfig(
|
|
{ projectRef }: AuthConfigVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get('/platform/auth/{ref}/config', {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type ProjectAuthConfigData = Awaited<ReturnType<typeof getProjectAuthConfig>>
|
|
export type ProjectAuthConfigError = ResponseError
|
|
|
|
export const useAuthConfigQuery = <TData = ProjectAuthConfigData>(
|
|
{ projectRef }: AuthConfigVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<ProjectAuthConfigData, ProjectAuthConfigError, TData> = {}
|
|
) =>
|
|
useQuery<ProjectAuthConfigData, ProjectAuthConfigError, TData>({
|
|
queryKey: authKeys.authConfig(projectRef),
|
|
queryFn: ({ signal }) => getProjectAuthConfig({ projectRef }, signal),
|
|
enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined' && projectRef !== '_',
|
|
...options,
|
|
})
|
|
|
|
export const useAuthConfigPrefetch = ({ projectRef }: AuthConfigVariables) => {
|
|
const client = useQueryClient()
|
|
|
|
return useCallback(() => {
|
|
if (projectRef) {
|
|
client.prefetchQuery({
|
|
queryKey: authKeys.authConfig(projectRef),
|
|
queryFn: ({ signal }) => getProjectAuthConfig({ projectRef }, signal),
|
|
})
|
|
}
|
|
}, [client, projectRef])
|
|
}
|