Files
2026-11-23 14:51:03 +00:00

40 lines
1.5 KiB
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import i18next from 'i18next';
import { toast } from 'sonner';
import { setCurrentUserQueryData } from '@/api/account/queries';
import {
clientDisableTwoFactorAuthenticationMutation,
clientEnableTwoFactorAuthenticationMutation,
clientGetTwoFactorSetupOptions,
} from '@/api/generated/@tanstack/react-query.gen';
import { notifyHttpError } from '@/plugins/notifications';
export const useAccountTwoFactorToken = (enabled = true) => useQuery({ ...clientGetTwoFactorSetupOptions(), enabled });
export const useEnableAccountTwoFactor = () => {
const queryClient = useQueryClient();
return useMutation({
...clientEnableTwoFactorAuthenticationMutation(),
onSuccess: () => {
setCurrentUserQueryData(queryClient, { useTotp: true });
toast.success(i18next.t('Two-step verification enabled'));
},
onError: (error) => notifyHttpError(error, i18next.t('Unable to enable two-step verification')),
});
};
export const useDisableAccountTwoFactor = () => {
const queryClient = useQueryClient();
return useMutation({
...clientDisableTwoFactorAuthenticationMutation(),
onSuccess: () => {
setCurrentUserQueryData(queryClient, { useTotp: false });
toast.success(i18next.t('Two-step verification disabled'));
},
onError: (error) => notifyHttpError(error, i18next.t('Unable to disable two-step verification')),
});
};