From bf2a59889139eed71ef904c3e20995174223f764 Mon Sep 17 00:00:00 2001 From: justme Date: Thu, 22 Oct 2026 05:59:56 +0800 Subject: [PATCH] web: toast passkey registration failures --- web/admin/src/pages/PasskeyAdd.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/web/admin/src/pages/PasskeyAdd.tsx b/web/admin/src/pages/PasskeyAdd.tsx index bd1cb61..c948253 100644 --- a/web/admin/src/pages/PasskeyAdd.tsx +++ b/web/admin/src/pages/PasskeyAdd.tsx @@ -1,7 +1,7 @@ import { Plus } from 'lucide-react' import { useState } from 'react' import { passkeySupported, registerPasskey, type PasskeyPlace } from '@shared/api/webauthn' -import { guessPlatform, useAction, useToast } from '@shared/ui' +import { describeError, guessPlatform, useAction, useToast } from '@shared/ui' /** 默认的 passkey 名称:按浏览器所在的系统给一个能认出来的名字。 */ function defaultName(): string { @@ -38,10 +38,18 @@ export function PasskeyAddForm({ onAdded, compact }: { onAdded: () => void; comp try { added = (await registerPasskey(name.trim(), place)).name } catch (e) { - if (e instanceof DOMException && e.name === 'NotAllowedError') throw new Error('已取消') - throw e + if (e instanceof DOMException && e.name === 'NotAllowedError') { + toast('在浏览器的提示里取消了,或者超时了。可以重新点「添加 Passkey」再试一次。', true, { title: 'Passkey 没有添加', ms: 10_000 }) + } else { + // 失败也像成功一样弹出来、停 10 秒:写清楚原因和常见的排查方向,点一下可以提前关掉 + toast(`${describeError(e)}。常见原因:页面地址(来源)和面板配置的 WebAuthn 来源不一致、所选保存位置不被浏览器支持、没有通过 HTTPS 或 localhost 打开。`, true, { title: 'Passkey 添加失败', ms: 10_000 }) + } + + return false } - }) + + return true + }).then((done) => done && added !== undefined) if (ok) { // 成功提示停 10 秒:说清楚存了什么、下一步怎么用,点一下可以提前关掉 toast(`「${added}」已经保存。下次登录时点用户名框选它,或者点「使用 Passkey 登录」;建议再添加一个备用的。`, false, { title: 'Passkey 添加成功', ms: 10_000 })