|
1 | 1 | <script lang="ts"> |
2 | 2 | import KeyRound from '@lucide/svelte/icons/key-round'; |
| 3 | + import { accountV1Api } from '$lib/api'; |
3 | 4 | import PasswordInput from '$lib/components/input/PasswordInput.svelte'; |
4 | 5 | import { Button } from '$lib/components/ui/button'; |
| 6 | + import { handleApiError } from '$lib/errorhandling/apiErrorHandling'; |
5 | 7 | import { validatePasswordMatch } from '$lib/inputvalidation/passwordValidator'; |
| 8 | + import { toast } from 'svelte-sonner'; |
| 9 | +
|
| 10 | + let loading = $state<boolean>(false); |
6 | 11 |
|
7 | 12 | let currentPassword = $state<string>(''); |
8 | 13 | let currentPasswordValid = $derived(currentPassword.length > 0); |
|
11 | 16 | let passwordValid = $state<boolean>(false); |
12 | 17 |
|
13 | 18 | let passwordConfirm = $state<string>(''); |
| 19 | + let passwordConfirmValid = $derived(validatePasswordMatch(passwordConfirm, password)); |
| 20 | +
|
| 21 | + async function submitPassword(e: SubmitEvent) { |
| 22 | + e.preventDefault(); |
| 23 | + loading = true; |
| 24 | +
|
| 25 | + try { |
| 26 | + await accountV1Api.authenticatedAccountChangePassword({ |
| 27 | + currentPassword, |
| 28 | + newPassword: password, |
| 29 | + }); |
| 30 | +
|
| 31 | + toast.success('Password has been changed'); |
14 | 32 |
|
15 | | - function submitPassword() { |
16 | | - console.log('Submitting password'); |
| 33 | + currentPassword = ''; |
| 34 | + password = ''; |
| 35 | + passwordConfirm = ''; |
| 36 | + } catch (e) { |
| 37 | + await handleApiError(e); |
| 38 | + } finally { |
| 39 | + loading = false; |
| 40 | + } |
17 | 41 | } |
18 | 42 |
|
19 | 43 | let canSubmitPassword = $derived( |
20 | | - currentPasswordValid && passwordValid && password == passwordConfirm |
| 44 | + currentPasswordValid && passwordValid && (passwordConfirmValid?.valid ?? false) && !loading |
21 | 45 | ); |
22 | 46 | </script> |
23 | 47 |
|
24 | | -<PasswordInput |
25 | | - label="Current Password" |
26 | | - placeholder="Current Password" |
27 | | - autocomplete="off" |
28 | | - bind:value={currentPassword} |
29 | | - Icon={KeyRound} |
30 | | -/> |
31 | | - |
32 | | -<PasswordInput |
33 | | - label="New Password" |
34 | | - placeholder="New Password" |
35 | | - autocomplete="new-password" |
36 | | - bind:value={password} |
37 | | - bind:valid={passwordValid} |
38 | | - Icon={KeyRound} |
39 | | - validate |
40 | | - showStrengthMeter |
41 | | -/> |
42 | | - |
43 | | -<PasswordInput |
44 | | - label="Confirm New Password" |
45 | | - placeholder="Confirm New Password" |
46 | | - autocomplete="new-password" |
47 | | - bind:value={passwordConfirm} |
48 | | - Icon={KeyRound} |
49 | | - validate={validatePasswordMatch(passwordConfirm, password)} |
50 | | -/> |
51 | | - |
52 | | -<Button type="submit" disabled={!canSubmitPassword}>Change Password</Button> |
| 48 | +<form class="w-full" onsubmit={submitPassword}> |
| 49 | + <PasswordInput |
| 50 | + label="Current Password" |
| 51 | + placeholder="Current Password" |
| 52 | + autocomplete="off" |
| 53 | + bind:value={currentPassword} |
| 54 | + Icon={KeyRound} |
| 55 | + /> |
| 56 | + |
| 57 | + <PasswordInput |
| 58 | + label="New Password" |
| 59 | + placeholder="New Password" |
| 60 | + autocomplete="new-password" |
| 61 | + bind:value={password} |
| 62 | + bind:valid={passwordValid} |
| 63 | + Icon={KeyRound} |
| 64 | + validate |
| 65 | + showStrengthMeter |
| 66 | + /> |
| 67 | + |
| 68 | + <PasswordInput |
| 69 | + label="Confirm New Password" |
| 70 | + placeholder="Confirm New Password" |
| 71 | + autocomplete="new-password" |
| 72 | + bind:value={passwordConfirm} |
| 73 | + Icon={KeyRound} |
| 74 | + validate={passwordConfirmValid} |
| 75 | + /> |
| 76 | + |
| 77 | + <Button type="submit" disabled={!canSubmitPassword}>Change Password</Button> |
| 78 | +</form> |
0 commit comments