Files
supabase/apps/studio/data/auth/auth-config-update-mutation.ts
Joshen Lim 71acf22318 Skip invalidating auth config when toggling security email notification in template editor page (#44888)
## Context

Preferred fix to this PR
[here](https://github.com/supabase/supabase/pull/44886)

On a security email template page (e.g. Reauthentication), if you edit
the Subject or Body without saving, then toggle **Enable notification**
in the Configuration card and click **Save changes**, the Content edits
are reverted to the last saved values.

## Changes involved

Opting to not invalidate the `authConfig` RQ cache on a successful
mutation when toggling the security email notification while on the
template editor page

## To test

- [ ] Open a security email template
- [ ] Make some changes to the template without saving 
- [ ] Toggle the notification and save
- [ ] Ensure that the template changes are still present and not
reverted

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Optimized query cache handling for authentication configuration
updates to reduce unnecessary refresh operations.
* Enhanced type safety for form submissions in authentication settings
to prevent data validation errors.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-04-16 09:27:57 +10:00

67 lines
2.0 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
await Promise.all([
!skipInvalidation &&
queryClient.invalidateQueries({ queryKey: authKeys.authConfig(projectRef) }),
queryClient.invalidateQueries({ queryKey: lintKeys.lint(projectRef) }),
])
await queryClient.refetchQueries({
queryKey: lintKeys.lint(projectRef),
type: 'active',
})
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update auth configuration: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}