Files
supabase/apps/studio/data/profile/profile-unlink-identity-mutation.ts
T
Joshen Lim 40ca927808 Chore/support updating email and unlinking identities (#33966)
* Support updating email address for email identity

* Support unlinking identity

* Deprecate AccountInformation component

* Invalidate identities after unlinking

* Address feedback

* Smol

* Fix type issues

* Update toast message

* Set up email change pending badge

* Fix

* Address feedback

* Resolve ts-expect-errors

* Fix TS issues + Fix GH icon on light mode + Address feedback
2025-03-21 17:15:05 +08:00

45 lines
1.2 KiB
TypeScript

import type { UserIdentity } from '@supabase/supabase-js'
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { auth } from 'lib/gotrue'
import { profileKeys } from './keys'
const unlinkIdentity = async (identity: UserIdentity) => {
const { error, data } = await auth.unlinkIdentity(identity)
if (error) throw error
return data
}
type UnlinkIdentityResponse = any
type UnlinkIdentityError = any
export const useUnlinkIdentityMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UnlinkIdentityResponse, UnlinkIdentityError, UserIdentity>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation((vars) => unlinkIdentity(vars), {
async onSuccess(data, variables, context) {
await Promise.all([
auth.refreshSession(),
queryClient.invalidateQueries(profileKeys.identities()),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to unlink identity: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}