diff --git a/composer.json b/composer.json index 7fc9bf2..97ecc9b 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ } }, "require": { - "saucebase/core": "^2.7", + "saucebase/core": "^2.8", "laravel/socialite": "^5.31", "stechstudio/filament-impersonate": "^5.6.1", "laravel/passkeys": "^0.2.1" diff --git a/resources/js/lib/passkeyError.test.ts b/resources/js/lib/passkeyError.test.ts new file mode 100644 index 0000000..96db24a --- /dev/null +++ b/resources/js/lib/passkeyError.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from 'vitest'; +import { passkeyErrorMessage } from './passkeyError'; + +const t = (key: string) => `t:${key}`; + +describe('passkeyErrorMessage', () => { + it('says nothing without an error', () => { + expect(passkeyErrorMessage(null, t)).toBeNull(); + expect(passkeyErrorMessage(undefined, t)).toBeNull(); + }); + + it('says nothing when the user closed the prompt', () => { + expect( + passkeyErrorMessage( + { name: 'UserCancelledError', message: 'Cancelled' }, + t, + ), + ).toBeNull(); + }); + + it('points to another device when this one already has a passkey', () => { + expect( + passkeyErrorMessage( + { + name: 'PasskeyExistsError', + message: 'This device is already registered as a passkey.', + }, + t, + ), + ).toBe( + 't:This device already has a passkey. Add one from another device or a password manager.', + ); + }); + + it('passes any other error through', () => { + expect( + passkeyErrorMessage({ name: 'Error', message: 'Server error' }, t), + ).toBe('Server error'); + }); +}); diff --git a/resources/js/lib/passkeyError.ts b/resources/js/lib/passkeyError.ts new file mode 100644 index 0000000..c931046 --- /dev/null +++ b/resources/js/lib/passkeyError.ts @@ -0,0 +1,23 @@ +/** + * What to tell the user when adding a passkey fails, or null to say nothing. + * + * Closing the browser's own prompt is a choice, not something to report. The + * package's "already registered" message is English only and says nothing about + * what to do next: the browser refuses a second passkey on the same authenticator. + */ +export function passkeyErrorMessage( + error: { name: string; message: string } | null | undefined, + t: (key: string) => string, +): string | null { + if (!error || error.name === 'UserCancelledError') { + return null; + } + + if (error.name === 'PasskeyExistsError') { + return t( + 'This device already has a passkey. Add one from another device or a password manager.', + ); + } + + return error.message; +} diff --git a/resources/js/react/pages/SettingsSecurity.tsx b/resources/js/react/pages/SettingsSecurity.tsx index 45e0413..236730a 100644 --- a/resources/js/react/pages/SettingsSecurity.tsx +++ b/resources/js/react/pages/SettingsSecurity.tsx @@ -1,6 +1,7 @@ import { Button } from '@/components/ui/button'; import { Field, FieldError, FieldLabel } from '@/components/ui/field'; import { Input } from '@/components/ui/input'; +import { EmptyState } from '@/components/ui/saucebase'; import { Separator } from '@/components/ui/separator'; import { Form, router, usePage } from '@inertiajs/react'; import { usePasskeyRegister } from '@laravel/passkeys/react'; @@ -10,6 +11,7 @@ import { useT, useTranslation } from '@saucebase/react/i18n'; import { Eye, EyeOff, KeyRound, Loader2, Trash2 } from 'lucide-react'; import { useEffect, useMemo, useRef, useState } from 'react'; import { deviceName } from '../../lib/deviceName'; +import { passkeyErrorMessage } from '../../lib/passkeyError'; import { confirmedFor, isConfirmed } from '../../lib/passwordConfirmation'; import { ConfirmIdentityDialog } from '../components/ConfirmIdentityDialog'; import { providerIcons } from '../components/providerIcons'; @@ -209,11 +211,7 @@ export default function SettingsSecurity({ if (addPasskeyError?.name === 'Error') confirmedUntil.current = null; }, [addPasskeyError]); - // Closing the browser's own prompt is a choice, not something to report. - const addPasskeyErrorMessage = - addPasskeyError?.name === 'UserCancelledError' - ? null - : addPasskeyError?.message; + const addPasskeyErrorMessage = passkeyErrorMessage(addPasskeyError, t); /** * Shown while the feature is on, and also while the user still holds @@ -364,69 +362,9 @@ export default function SettingsSecurity({ )}

- {/* Password */} -
-
-

{t('Update Password')}

-

- {t( - 'Ensure your account is using a long, random password to stay secure.', - )} -

-
- -
- {({ errors }) => ( - <> - - - - - - -
- -
- - )} - -
- {/* Passkeys */} {showPasskeys && ( <> - -

{t('Passkeys')}

@@ -546,22 +484,76 @@ export default function SettingsSecurity({ ))}
) : ( -
} data-testid="passkeys-empty" - > -
- -
-

- {t('No passkeys yet.')} -

-
+ /> )}
+ + )} + {/* Password */} +
+
+

{t('Update Password')}

+

+ {t( + 'Ensure your account is using a long, random password to stay secure.', + )} +

+
+ +
+ {({ errors }) => ( + <> + + + + + + +
+ +
+ + )} + +
+ {/* Connected accounts */} {hasSocialiteProviders && ( <> diff --git a/resources/js/vue/pages/SettingsSecurity.vue b/resources/js/vue/pages/SettingsSecurity.vue index 5cd94c9..eb655c7 100644 --- a/resources/js/vue/pages/SettingsSecurity.vue +++ b/resources/js/vue/pages/SettingsSecurity.vue @@ -1,4 +1,5 @@