mirror of
https://github.com/supabase/supabase.git
synced 2026-06-14 23:25:16 +08:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES/NO ## What kind of change does this PR introduce? Bug fix, feature, docs update, ... ## What is the current behavior? Please link any relevant issues here. ## What is the new behavior? Feel free to include screenshots if it includes visual changes. ## Additional context Add any other context or screenshots. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Authentication configuration updates now trigger the app's default refresh behavior, ensuring changes propagate automatically and remain synchronized across the interface. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { authKeys } from './keys'
|
|
import type { components } from '@/data/api'
|
|
import { handleError, patch } from '@/data/fetchers'
|
|
import { lintKeys } from '@/data/lint/keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type AuthConfigUpdateVariables = {
|
|
projectRef: string
|
|
config: Partial<components['schemas']['UpdateGoTrueConfigBody']>
|
|
skipInvalidation?: boolean
|
|
}
|
|
|
|
export async function updateAuthConfig({ projectRef, config }: AuthConfigUpdateVariables) {
|
|
const { data, error } = await patch('/platform/auth/{ref}/config', {
|
|
params: {
|
|
path: { ref: projectRef },
|
|
},
|
|
body: {
|
|
...config,
|
|
},
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type AuthConfigUpdateData = Awaited<ReturnType<typeof updateAuthConfig>>
|
|
|
|
export const useAuthConfigUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<AuthConfigUpdateData, ResponseError, AuthConfigUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<AuthConfigUpdateData, ResponseError, AuthConfigUpdateVariables>({
|
|
mutationFn: (vars) => updateAuthConfig(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, skipInvalidation = false } = variables
|
|
|
|
if (!skipInvalidation) {
|
|
await queryClient.invalidateQueries({
|
|
queryKey: authKeys.authConfig(projectRef),
|
|
})
|
|
}
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
|
|
queryClient
|
|
.invalidateQueries({ queryKey: lintKeys.lint(projectRef) })
|
|
.then(() =>
|
|
queryClient.refetchQueries({
|
|
queryKey: lintKeys.lint(projectRef),
|
|
type: 'active',
|
|
})
|
|
)
|
|
.catch(() => undefined)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update auth configuration: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|