Files
supabase/studio/data/auth/user-reset-password-mutation.ts
Joshen Lim 95344c246b Chore/deprecate local observable (#17642)
* Deprecate local observable in api.tsx

* Deprecate local observable in auth users pqage

* Replace prop drilling refetch with proper query invalidation in mutations
2023-09-26 13:25:38 +08:00

48 lines
1.4 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { ResponseError } from 'types'
import { User } from './users-query'
export type UserResetPasswordVariables = {
projectRef: string
user: User
}
export async function resetPassword({ projectRef, user }: UserResetPasswordVariables) {
const response = await post(`${API_URL}/auth/${projectRef}/recover`, user)
if (response.error) throw response.error
return response
}
type UserResetPasswordData = Awaited<ReturnType<typeof resetPassword>>
export const useUserResetPasswordMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UserResetPasswordData, ResponseError, UserResetPasswordVariables>,
'mutationFn'
> = {}) => {
return useMutation<UserResetPasswordData, ResponseError, UserResetPasswordVariables>(
(vars) => resetPassword(vars),
{
async onSuccess(data, variables, context) {
// [Joshen] If we need to invalidate any queries
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to reset user password: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}