web: toast passkey registration failures
This commit is contained in:
1 parent
758b6b1566
commit
bf2a598891
1 file changed
+12
-4
@@ -1,7 +1,7 @@
|
|||||||
import { Plus } from 'lucide-react'
|
import { Plus } from 'lucide-react'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { passkeySupported, registerPasskey, type PasskeyPlace } from '@shared/api/webauthn'
|
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 名称:按浏览器所在的系统给一个能认出来的名字。 */
|
/** 默认的 passkey 名称:按浏览器所在的系统给一个能认出来的名字。 */
|
||||||
function defaultName(): string {
|
function defaultName(): string {
|
||||||
@@ -38,10 +38,18 @@ export function PasskeyAddForm({ onAdded, compact }: { onAdded: () => void; comp
|
|||||||
try {
|
try {
|
||||||
added = (await registerPasskey(name.trim(), place)).name
|
added = (await registerPasskey(name.trim(), place)).name
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof DOMException && e.name === 'NotAllowedError') throw new Error('已取消')
|
if (e instanceof DOMException && e.name === 'NotAllowedError') {
|
||||||
throw e
|
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) {
|
if (ok) {
|
||||||
// 成功提示停 10 秒:说清楚存了什么、下一步怎么用,点一下可以提前关掉
|
// 成功提示停 10 秒:说清楚存了什么、下一步怎么用,点一下可以提前关掉
|
||||||
toast(`「${added}」已经保存。下次登录时点用户名框选它,或者点「使用 Passkey 登录」;建议再添加一个备用的。`, false, { title: 'Passkey 添加成功', ms: 10_000 })
|
toast(`「${added}」已经保存。下次登录时点用户名框选它,或者点「使用 Passkey 登录」;建议再添加一个备用的。`, false, { title: 'Passkey 添加成功', ms: 10_000 })
|
||||||
|
|||||||
Reference in new issue
Block a user