import { useState } from "react"; import { invoke } from "@tauri-apps/api/core"; import type { AuthenticationType, ConnectionProfile } from "../../types/profile"; interface CredentialEnrollmentResult { profileId: string; credentialReference: string; authenticationType: AuthenticationType; } interface Props { profile: ConnectionProfile; onChanged: () => Promise | void; } export function CredentialEnrollmentCard({ profile, onChanged }: Props) { const [secret, setSecret] = useState(""); const [username, setUsername] = useState(profile.remoteUsername ?? ""); const [busy, setBusy] = useState(false); const [message, setMessage] = useState(null); async function enroll() { setBusy(true); setMessage(null); try { const result = await invoke( "enroll_profile_credential", { request: { profileId: profile.id, authenticationType: profile.authenticationType, secret, username: username.trim() || undefined, }, }, ); setSecret(""); setMessage(`已登记凭据引用 ${result.credentialReference}`); await onChanged(); } catch (error) { setMessage( error instanceof Error ? error.message : "凭据登记失败(详情未包含密钥)。", ); } finally { setBusy(false); } } async function clearCredential() { setBusy(true); setMessage(null); try { await invoke("clear_profile_credential", { profileId: profile.id }); setMessage("已清除凭据引用"); await onChanged(); } catch (error) { setMessage( error instanceof Error ? error.message : "清除凭据失败。", ); } finally { setBusy(false); } } return (

安全凭据登记

密钥只通过一次性 IPC 写入 Windows Credential Manager,不会进入 React 持久状态。

{profile.credentialReference ? (

当前引用:{profile.credentialReference}

) : (

尚未登记凭据

)} {message ?

{message}

: null}
); }