Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

1.5 KB
Raw Blame
import { useCallback } from "react";
import { useQueryClient } from "@tanstack/react-query";

import { createNotification } from "@app/components/notifications";
import { apiRequest } from "@app/config/request";
import { userKeys } from "@app/hooks/api/users/query-keys";
import { webAuthnKeys } from "@app/hooks/api/webauthn/queries";

import { useMfaStepUp } from "./useMfaStepUp";

/**
 * Removing a passkey weakens a login second factor, so it is gated behind a fresh
 * step-up MFA challenge (the same primitive used to view/rotate recovery codes).
 * Calls apiRequest directly (rather than a mutation) so the expected
 * SESSION_MFA_REQUIRED 400 doesn't surface a global mutation error toast.
 */
export const useRemovePasskey = () => {
  const queryClient = useQueryClient();
  const { isBusy, runWithMfa } = useMfaStepUp();

  const removePasskey = useCallback(
    (id: string, onRemoved?: () => void) =>
      runWithMfa(
        async (mfaSessionId) => {
          await apiRequest.delete(`/api/v1/user/me/webauthn/${id}`, {
            params: mfaSessionId ? { mfaSessionId } : undefined
          });
        },
        {
          onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: webAuthnKeys.credentials });
            queryClient.invalidateQueries({ queryKey: userKeys.getUser });
            createNotification({ text: "Passkey removed", type: "success" });
            onRemoved?.();
          },
          errorText: "Failed to remove passkey"
        }
      ),
    [runWithMfa, queryClient]
  );

  return { isBusy, removePasskey };
};