feat(sync): paginate Android sync with change tokens
This commit is contained in:
1 parent
0ff94f9b10
commit
97c237be80
76 files changed
+3401
-851
No files matched your search
@@ -13,6 +13,7 @@
|
||||
onTotpSetup,
|
||||
onTotpStatus,
|
||||
totpEnabled = false,
|
||||
requireCredentialsForEmailChange = false,
|
||||
open = $bindable(false)
|
||||
}: {
|
||||
currentEmail?: string;
|
||||
@@ -23,6 +24,7 @@
|
||||
onTotpSetup?: TotpSetupRequest;
|
||||
onTotpStatus?: TotpStatusRequest;
|
||||
totpEnabled?: boolean;
|
||||
requireCredentialsForEmailChange?: boolean;
|
||||
open?: boolean;
|
||||
} = $props();
|
||||
|
||||
@@ -30,8 +32,13 @@
|
||||
let email = $state('');
|
||||
let password = $state('');
|
||||
let confirmPassword = $state('');
|
||||
let currentPassword = $state('');
|
||||
let totpToken = $state('');
|
||||
let error = $state('');
|
||||
let saving = $state(false);
|
||||
let requiresCurrentCredentials = $derived(
|
||||
!!password || (requireCredentialsForEmailChange && email.trim() !== currentEmail.trim())
|
||||
);
|
||||
|
||||
$effect(() => {
|
||||
if (open) {
|
||||
@@ -39,6 +46,8 @@
|
||||
email = currentEmail;
|
||||
password = '';
|
||||
confirmPassword = '';
|
||||
currentPassword = '';
|
||||
totpToken = '';
|
||||
error = '';
|
||||
}
|
||||
});
|
||||
@@ -52,6 +61,16 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if (requiresCurrentCredentials && !currentPassword) {
|
||||
error = 'Enter your current password to change your email or password.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (requiresCurrentCredentials && totpEnabled && !totpToken) {
|
||||
error = 'Enter a current authenticator code to change your email or password.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (!onSave) {
|
||||
error = 'The application has not configured its profile update handler.';
|
||||
return;
|
||||
@@ -59,7 +78,13 @@
|
||||
|
||||
saving = true;
|
||||
try {
|
||||
await onSave({ username, email, password: password || undefined });
|
||||
const input: ProfileUpdateInput = { username, email };
|
||||
if (requiresCurrentCredentials) {
|
||||
input.currentPassword = currentPassword;
|
||||
if (totpEnabled) input.totpToken = totpToken;
|
||||
}
|
||||
if (password) input.password = password;
|
||||
await onSave(input);
|
||||
open = false;
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Profile update failed.';
|
||||
@@ -100,6 +125,35 @@
|
||||
/>
|
||||
</label>
|
||||
|
||||
{#if requiresCurrentCredentials}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Current password</span>
|
||||
<input
|
||||
autocomplete="current-password"
|
||||
class="sc-input"
|
||||
required
|
||||
type="password"
|
||||
bind:value={currentPassword}
|
||||
/>
|
||||
</label>
|
||||
|
||||
{#if totpEnabled}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Current authenticator code</span>
|
||||
<input
|
||||
autocomplete="one-time-code"
|
||||
class="sc-input"
|
||||
inputmode="numeric"
|
||||
maxlength="6"
|
||||
pattern="[0-9]{6}"
|
||||
required
|
||||
type="text"
|
||||
bind:value={totpToken}
|
||||
/>
|
||||
</label>
|
||||
{/if}
|
||||
{/if}
|
||||
|
||||
{#if password}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Confirm new password</span>
|
||||
|
||||
@@ -42,6 +42,8 @@ export interface ProfileUpdateInput {
|
||||
username: string;
|
||||
email: string;
|
||||
password?: string;
|
||||
currentPassword?: string;
|
||||
totpToken?: string;
|
||||
}
|
||||
|
||||
export type UserRole = "superadmin" | "admin" | "user";
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import ProfileDialog from '../src/lib/components/ProfileDialog.svelte';
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
const fieldLabels: Record<string, string> = {
|
||||
username: 'Username',
|
||||
email: 'Email',
|
||||
password: 'New password',
|
||||
confirmPassword: 'Confirm new password',
|
||||
currentPassword: 'Current password',
|
||||
totpToken: 'Current authenticator code',
|
||||
};
|
||||
|
||||
const field = (name: string): HTMLInputElement => screen.getByLabelText(fieldLabels[name]) as HTMLInputElement;
|
||||
|
||||
async function fill(name: string, value: string): Promise<void> {
|
||||
await fireEvent.input(field(name), { target: { value } });
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
const form = document.querySelector('form');
|
||||
if (!form) throw new Error('no form');
|
||||
await fireEvent.submit(form);
|
||||
}
|
||||
|
||||
describe('ProfileDialog password changes', () => {
|
||||
it('requires current password and a current TOTP code when enabled', async () => {
|
||||
const onSave = vi.fn().mockResolvedValue(undefined);
|
||||
render(ProfileDialog, { props: { open: true, onSave, totpEnabled: true } });
|
||||
await fill('username', 'alice');
|
||||
await fill('email', 'alice@example.com');
|
||||
await fill('password', 'new-password');
|
||||
await fill('confirmPassword', 'new-password');
|
||||
|
||||
await submit();
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByText('Enter your current password to change it.')).toBeTruthy();
|
||||
|
||||
await fill('currentPassword', 'old-password');
|
||||
await submit();
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByText('Enter a current authenticator code to change your password.')).toBeTruthy();
|
||||
|
||||
await fill('totpToken', '123456');
|
||||
await submit();
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
|
||||
expect(onSave).toHaveBeenCalledWith({
|
||||
username: 'alice',
|
||||
email: 'alice@example.com',
|
||||
password: 'new-password',
|
||||
currentPassword: 'old-password',
|
||||
totpToken: '123456',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not ask for password proof when only profile fields change', async () => {
|
||||
const onSave = vi.fn().mockResolvedValue(undefined);
|
||||
render(ProfileDialog, { props: { open: true, onSave, totpEnabled: true } });
|
||||
await fill('username', 'alice');
|
||||
await fill('email', 'alice@example.com');
|
||||
await submit();
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
|
||||
expect(onSave).toHaveBeenCalledWith({ username: 'alice', email: 'alice@example.com' });
|
||||
expect(screen.queryByLabelText('Current password')).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user