mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
Add query and mutation for fetching and mutation auth config.
This commit is contained in:
1 parent
9d3dc861b8
commit
8bb2dd1bb4
3 files changed
+120
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
|
||||
import { components } from 'data/api'
|
||||
import { get, isResponseOk } from 'lib/common/fetch'
|
||||
import { API_URL } from 'lib/constants'
|
||||
import { useCallback } from 'react'
|
||||
import { authKeys } from './keys'
|
||||
|
||||
export type AuthConfigVariables = {
|
||||
projectRef?: string
|
||||
}
|
||||
|
||||
export type AuthConfigResponse = components['schemas']['GetGoTrueConfigResponse']
|
||||
|
||||
export async function getProjectAuthConfig(
|
||||
{ projectRef }: AuthConfigVariables,
|
||||
signal?: AbortSignal
|
||||
) {
|
||||
if (!projectRef) {
|
||||
throw new Error('projectRef is required')
|
||||
}
|
||||
|
||||
const response = await get<AuthConfigResponse>(`${API_URL}/auth/${projectRef}/config`, {
|
||||
signal,
|
||||
})
|
||||
if (!isResponseOk(response)) {
|
||||
throw response.error
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export type ProjectAuthConfigData = Awaited<ReturnType<typeof getProjectAuthConfig>>
|
||||
export type ProjectAuthConfigError = unknown
|
||||
|
||||
export const useAuthConfigQuery = <TData = ProjectAuthConfigData>(
|
||||
{ projectRef }: AuthConfigVariables,
|
||||
{
|
||||
enabled = true,
|
||||
...options
|
||||
}: UseQueryOptions<ProjectAuthConfigData, ProjectAuthConfigError, TData> = {}
|
||||
) =>
|
||||
useQuery<ProjectAuthConfigData, ProjectAuthConfigError, TData>(
|
||||
authKeys.authConfig(projectRef),
|
||||
({ signal }) => getProjectAuthConfig({ projectRef }, signal),
|
||||
{
|
||||
enabled: enabled && typeof projectRef !== 'undefined',
|
||||
...options,
|
||||
}
|
||||
)
|
||||
|
||||
export const useAuthConfigPrefetch = ({ projectRef }: AuthConfigVariables) => {
|
||||
const client = useQueryClient()
|
||||
|
||||
return useCallback(() => {
|
||||
if (projectRef) {
|
||||
client.prefetchQuery(authKeys.authConfig(projectRef), ({ signal }) =>
|
||||
getProjectAuthConfig({ projectRef }, signal)
|
||||
)
|
||||
}
|
||||
}, [client, projectRef])
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
||||
import toast from 'react-hot-toast'
|
||||
|
||||
import { components } from 'data/api'
|
||||
import { patch } from 'lib/common/fetch'
|
||||
import { API_URL } from 'lib/constants'
|
||||
import { ResponseError } from 'types'
|
||||
import { authKeys } from './keys'
|
||||
|
||||
export type AuthConfigUpdateVariables = {
|
||||
projectRef: string
|
||||
config: Partial<components['schemas']['UpdateGoTrueConfigBody']>
|
||||
}
|
||||
|
||||
export type UpdateAuthConfigResponse = components['schemas']['GoTrueConfig']
|
||||
|
||||
export async function updateAuthConfig({ projectRef, config }: AuthConfigUpdateVariables) {
|
||||
const response = await patch(`${API_URL}/auth/${projectRef}/config`, { ...config })
|
||||
if (response.error) {
|
||||
throw response.error
|
||||
}
|
||||
|
||||
return response as UpdateAuthConfigResponse
|
||||
}
|
||||
|
||||
type AuthConfigUpdateData = Awaited<ReturnType<typeof updateAuthConfig>>
|
||||
|
||||
export const useAuthConfigUpdateMutation = ({
|
||||
onSuccess,
|
||||
onError,
|
||||
...options
|
||||
}: Omit<
|
||||
UseMutationOptions<AuthConfigUpdateData, ResponseError, AuthConfigUpdateVariables>,
|
||||
'mutationFn'
|
||||
> = {}) => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation<AuthConfigUpdateData, ResponseError, AuthConfigUpdateVariables>(
|
||||
(vars) => updateAuthConfig(vars),
|
||||
{
|
||||
async onSuccess(data, variables, context) {
|
||||
const { projectRef } = variables
|
||||
|
||||
await Promise.all([queryClient.invalidateQueries(authKeys.authConfig(projectRef))])
|
||||
|
||||
await onSuccess?.(data, variables, context)
|
||||
},
|
||||
async onError(data, variables, context) {
|
||||
if (onError === undefined) {
|
||||
toast.error(`Failed to mutate: ${data.message}`)
|
||||
} else {
|
||||
onError(data, variables, context)
|
||||
}
|
||||
},
|
||||
...options,
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export const authKeys = {
|
||||
authConfig: (projectRef: string | undefined) => ['auth', projectRef, 'config'] as const,
|
||||
accessToken: () => ['access-token'] as const,
|
||||
}
|
||||
Reference in new issue
Block a user