feat(auth): implement TOTP setup and management
Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
- Added TOTP support for two-factor authentication in the auth module. - Introduced new types and interfaces for TOTP setup and token actions in the web UI. - Created backend logic for generating and verifying TOTP secrets and tokens. - Implemented user management features including role assignments and user CRUD operations. - Enhanced UI components for managing users and TOTP settings. - Added styles for optional labels and authentication reset links. - Implemented reset code generation and validation for admin accounts.
This commit is contained in:
1 parent
2f57554154
commit
da97fd4fb1
30 files changed
+2145
-27
No files matched your search
@@ -1,6 +1,11 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
import type { AuthLoginInput, AuthSetupInput } from '../types';
|
||||
import type {
|
||||
AuthLoginInput,
|
||||
AuthResetConfirm,
|
||||
AuthResetRequest,
|
||||
AuthSetupInput
|
||||
} from '../types';
|
||||
import Button from './Button.svelte';
|
||||
|
||||
let {
|
||||
@@ -9,7 +14,10 @@
|
||||
error = '',
|
||||
loading = false,
|
||||
onLogin,
|
||||
onConfirmReset,
|
||||
onRequestReset,
|
||||
onSetup,
|
||||
showTotp = true,
|
||||
setupRequired = false,
|
||||
tagline = ''
|
||||
}: {
|
||||
@@ -18,7 +26,10 @@
|
||||
error?: string;
|
||||
loading?: boolean;
|
||||
onLogin?: (input: AuthLoginInput) => void | Promise<void>;
|
||||
onConfirmReset?: AuthResetConfirm;
|
||||
onRequestReset?: AuthResetRequest;
|
||||
onSetup?: (input: AuthSetupInput) => void | Promise<void>;
|
||||
showTotp?: boolean;
|
||||
setupRequired?: boolean;
|
||||
tagline?: string;
|
||||
} = $props();
|
||||
@@ -27,9 +38,14 @@
|
||||
let email = $state('');
|
||||
let identifier = $state('');
|
||||
let password = $state('');
|
||||
let totpToken = $state('');
|
||||
let confirmPassword = $state('');
|
||||
let submitError = $state('');
|
||||
let submitting = $state(false);
|
||||
let resetCode = $state('');
|
||||
let resetError = $state('');
|
||||
let resetOpen = $state(false);
|
||||
let resetting = $state(false);
|
||||
|
||||
async function submit(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
@@ -48,7 +64,7 @@
|
||||
await onSetup({ username, email, password });
|
||||
} else {
|
||||
if (!onLogin) throw new Error('The application has not configured its login handler.');
|
||||
await onLogin({ identifier, password });
|
||||
await onLogin({ identifier, password, totpToken: totpToken.trim() });
|
||||
}
|
||||
} catch (caught) {
|
||||
submitError = caught instanceof Error ? caught.message : 'Authentication failed.';
|
||||
@@ -56,6 +72,38 @@
|
||||
submitting = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function requestReset(): Promise<void> {
|
||||
resetError = '';
|
||||
resetCode = '';
|
||||
resetOpen = true;
|
||||
|
||||
try {
|
||||
if (!onRequestReset) throw new Error('The application has not configured its reset handler.');
|
||||
await onRequestReset();
|
||||
} catch (caught) {
|
||||
resetError = caught instanceof Error ? caught.message : 'Unable to request an account reset.';
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmReset(): Promise<void> {
|
||||
resetError = '';
|
||||
const code = resetCode.trim();
|
||||
if (!code) {
|
||||
resetError = 'Enter the code printed in the server console.';
|
||||
return;
|
||||
}
|
||||
|
||||
resetting = true;
|
||||
try {
|
||||
if (!onConfirmReset) throw new Error('The application has not configured its reset handler.');
|
||||
await onConfirmReset(code);
|
||||
} catch (caught) {
|
||||
resetError = caught instanceof Error ? caught.message : 'Unable to reset the admin account.';
|
||||
} finally {
|
||||
resetting = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="sc-root sc-auth-page">
|
||||
@@ -122,6 +170,20 @@
|
||||
/>
|
||||
</label>
|
||||
|
||||
{#if showTotp && !setupRequired}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Authenticator code <span class="sc-label-optional">(if enabled)</span></span>
|
||||
<input
|
||||
autocomplete="one-time-code"
|
||||
class="sc-input"
|
||||
inputmode="numeric"
|
||||
name="totpToken"
|
||||
type="text"
|
||||
bind:value={totpToken}
|
||||
/>
|
||||
</label>
|
||||
{/if}
|
||||
|
||||
{#if setupRequired}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Confirm password</span>
|
||||
@@ -149,5 +211,35 @@
|
||||
{#if children}
|
||||
<div class="sc-auth-extra">{@render children()}</div>
|
||||
{/if}
|
||||
|
||||
{#if !setupRequired && onRequestReset && onConfirmReset}
|
||||
{#if resetOpen}
|
||||
<div class="sc-auth-reset" aria-live="polite">
|
||||
<p class="sc-auth-reset-hint">A reset code has been printed to the server console.</p>
|
||||
<input
|
||||
aria-label="Reset code"
|
||||
autocomplete="off"
|
||||
class="sc-input"
|
||||
placeholder="Enter reset code"
|
||||
type="text"
|
||||
bind:value={resetCode}
|
||||
/>
|
||||
{#if resetError}<p class="sc-error">{resetError}</p>{/if}
|
||||
<Button
|
||||
disabled={resetting}
|
||||
fullWidth
|
||||
size="sm"
|
||||
variant="danger"
|
||||
onclick={() => void confirmReset()}
|
||||
>
|
||||
{resetting ? 'Resetting…' : 'Confirm reset'}
|
||||
</Button>
|
||||
</div>
|
||||
{:else}
|
||||
<button class="sc-auth-reset-link" type="button" onclick={() => void requestReset()}>
|
||||
Reset admin account
|
||||
</button>
|
||||
{/if}
|
||||
{/if}
|
||||
</section>
|
||||
</div>
|
||||
@@ -1,17 +1,52 @@
|
||||
<script lang="ts">
|
||||
import type { ProfileUpdateInput } from '../types';
|
||||
import type {
|
||||
ProfileUpdateInput,
|
||||
TotpSetupRequest,
|
||||
TotpStatusRequest,
|
||||
TotpTokenAction,
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import Button from './Button.svelte';
|
||||
import Dialog from './Dialog.svelte';
|
||||
import TotpSettings from './TotpSettings.svelte';
|
||||
import UserManagement from './UserManagement.svelte';
|
||||
|
||||
let {
|
||||
currentEmail = '',
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
onCreateUser,
|
||||
onDeleteUser,
|
||||
onListUsers,
|
||||
currentUsername = '',
|
||||
onSave,
|
||||
onTotpDisable,
|
||||
onTotpEnable,
|
||||
onTotpSetup,
|
||||
onTotpStatus,
|
||||
onUpdateUser,
|
||||
totpEnabled = false,
|
||||
open = $bindable(false)
|
||||
}: {
|
||||
currentEmail?: string;
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
onCreateUser?: UserCreateRequest;
|
||||
onDeleteUser?: UserDeleteRequest;
|
||||
onListUsers?: UserListRequest;
|
||||
currentUsername?: string;
|
||||
onSave?: (input: ProfileUpdateInput) => void | Promise<void>;
|
||||
onTotpDisable?: TotpTokenAction;
|
||||
onTotpEnable?: TotpTokenAction;
|
||||
onTotpSetup?: TotpSetupRequest;
|
||||
onTotpStatus?: TotpStatusRequest;
|
||||
onUpdateUser?: UserUpdateRequest;
|
||||
totpEnabled?: boolean;
|
||||
open?: boolean;
|
||||
} = $props();
|
||||
|
||||
@@ -112,4 +147,24 @@
|
||||
<Button disabled={saving} type="submit">{saving ? 'Saving…' : 'Save changes'}</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{#if onTotpSetup && onTotpEnable && onTotpDisable}
|
||||
<TotpSettings
|
||||
{open}
|
||||
onDisable={onTotpDisable}
|
||||
onEnable={onTotpEnable}
|
||||
onSetup={onTotpSetup}
|
||||
onStatus={onTotpStatus}
|
||||
enabled={totpEnabled}
|
||||
/>
|
||||
{/if}
|
||||
|
||||
<UserManagement
|
||||
currentRole={currentRole}
|
||||
currentUserId={currentUserId}
|
||||
onCreate={onCreateUser}
|
||||
onDelete={onDeleteUser}
|
||||
onList={onListUsers}
|
||||
onUpdate={onUpdateUser}
|
||||
/>
|
||||
</Dialog>
|
||||
@@ -1,15 +1,36 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
import type {
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import Dialog from './Dialog.svelte';
|
||||
import UserManagement from './UserManagement.svelte';
|
||||
|
||||
let {
|
||||
children,
|
||||
description = '',
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
onCreateUser,
|
||||
onDeleteUser,
|
||||
onListUsers,
|
||||
onUpdateUser,
|
||||
open = $bindable(false),
|
||||
title = 'Settings'
|
||||
}: {
|
||||
children?: Snippet;
|
||||
description?: string;
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
onCreateUser?: UserCreateRequest;
|
||||
onDeleteUser?: UserDeleteRequest;
|
||||
onListUsers?: UserListRequest;
|
||||
onUpdateUser?: UserUpdateRequest;
|
||||
open?: boolean;
|
||||
title?: string;
|
||||
} = $props();
|
||||
@@ -21,4 +42,12 @@
|
||||
{:else}
|
||||
<p class="sc-muted">No settings have been configured for this application yet.</p>
|
||||
{/if}
|
||||
<UserManagement
|
||||
currentRole={currentRole}
|
||||
currentUserId={currentUserId}
|
||||
onCreate={onCreateUser}
|
||||
onDelete={onDeleteUser}
|
||||
onList={onListUsers}
|
||||
onUpdate={onUpdateUser}
|
||||
/>
|
||||
</Dialog>
|
||||
@@ -0,0 +1,167 @@
|
||||
<script lang="ts">
|
||||
import type {
|
||||
TotpSetupRequest,
|
||||
TotpStatusRequest,
|
||||
TotpTokenAction,
|
||||
TotpSetupResponse
|
||||
} from '../types';
|
||||
import Button from './Button.svelte';
|
||||
|
||||
let {
|
||||
enabled = false,
|
||||
onDisable,
|
||||
onEnable,
|
||||
onSetup,
|
||||
onStatus,
|
||||
open = false
|
||||
}: {
|
||||
enabled?: boolean;
|
||||
onDisable?: TotpTokenAction;
|
||||
onEnable?: TotpTokenAction;
|
||||
onSetup?: TotpSetupRequest;
|
||||
onStatus?: TotpStatusRequest;
|
||||
open?: boolean;
|
||||
} = $props();
|
||||
|
||||
let currentEnabled = $state(false);
|
||||
let setup = $state<TotpSetupResponse | null>(null);
|
||||
let token = $state('');
|
||||
let error = $state('');
|
||||
let busy = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
currentEnabled = enabled;
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (!open || !onStatus) return;
|
||||
void refreshStatus();
|
||||
});
|
||||
|
||||
async function refreshStatus(): Promise<void> {
|
||||
try {
|
||||
currentEnabled = await onStatus?.() ?? currentEnabled;
|
||||
} catch {
|
||||
// The explicit setup controls remain usable if status is unavailable.
|
||||
}
|
||||
}
|
||||
|
||||
async function beginSetup(): Promise<void> {
|
||||
if (!onSetup) return;
|
||||
error = '';
|
||||
busy = true;
|
||||
try {
|
||||
setup = await onSetup();
|
||||
token = '';
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to start two-factor setup.';
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmEnable(): Promise<void> {
|
||||
const normalized = token.trim();
|
||||
if (!/^\d{6}$/.test(normalized)) {
|
||||
error = 'Enter the six-digit code from your authenticator app.';
|
||||
return;
|
||||
}
|
||||
if (!onEnable) return;
|
||||
error = '';
|
||||
busy = true;
|
||||
try {
|
||||
await onEnable(normalized);
|
||||
currentEnabled = true;
|
||||
setup = null;
|
||||
token = '';
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to enable two-factor authentication.';
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function disable(): Promise<void> {
|
||||
const normalized = token.trim();
|
||||
if (!/^\d{6}$/.test(normalized)) {
|
||||
error = 'Enter the current six-digit authenticator code.';
|
||||
return;
|
||||
}
|
||||
if (!onDisable) return;
|
||||
error = '';
|
||||
busy = true;
|
||||
try {
|
||||
await onDisable(normalized);
|
||||
currentEnabled = false;
|
||||
token = '';
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to disable two-factor authentication.';
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<section class="sc-totp-settings" aria-labelledby="sc-totp-title">
|
||||
<div class="sc-totp-heading">
|
||||
<div>
|
||||
<h3 id="sc-totp-title">Two-factor authentication</h3>
|
||||
<p class="sc-muted">Protect sign-in with a time-based authenticator code.</p>
|
||||
</div>
|
||||
<span class:sc-totp-enabled={currentEnabled} class="sc-totp-status">
|
||||
{currentEnabled ? 'Enabled' : 'Optional'}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{#if currentEnabled}
|
||||
<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}"
|
||||
placeholder="123456"
|
||||
type="text"
|
||||
bind:value={token}
|
||||
/>
|
||||
</label>
|
||||
<Button disabled={busy} variant="danger" onclick={() => void disable()}>
|
||||
{busy ? 'Disabling…' : 'Disable two-factor authentication'}
|
||||
</Button>
|
||||
{:else if setup}
|
||||
<div class="sc-totp-secret">
|
||||
<p>Scan this address in your authenticator app, or enter the secret manually.</p>
|
||||
<code>{setup.otpauthUrl}</code>
|
||||
<p><span class="sc-label">Manual secret:</span> <code>{setup.secret}</code></p>
|
||||
</div>
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Authenticator code</span>
|
||||
<input
|
||||
autocomplete="one-time-code"
|
||||
class="sc-input"
|
||||
inputmode="numeric"
|
||||
maxlength="6"
|
||||
pattern="[0-9]{6}"
|
||||
placeholder="123456"
|
||||
type="text"
|
||||
bind:value={token}
|
||||
/>
|
||||
</label>
|
||||
<div class="sc-form-actions">
|
||||
<Button disabled={busy} variant="secondary" onclick={() => (setup = null)}>Cancel</Button>
|
||||
<Button disabled={busy} onclick={() => void confirmEnable()}>
|
||||
{busy ? 'Enabling…' : 'Enable two-factor authentication'}
|
||||
</Button>
|
||||
</div>
|
||||
{:else}
|
||||
<Button disabled={busy} onclick={() => void beginSetup()}>
|
||||
{busy ? 'Preparing…' : 'Set up authenticator'}
|
||||
</Button>
|
||||
{/if}
|
||||
|
||||
{#if error}
|
||||
<p aria-live="polite" class="sc-error">{error}</p>
|
||||
{/if}
|
||||
</section>
|
||||
@@ -0,0 +1,196 @@
|
||||
<script lang="ts">
|
||||
import type {
|
||||
ManagedUser,
|
||||
UserCreateInput,
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateInput,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import Button from './Button.svelte';
|
||||
|
||||
let {
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
onCreate,
|
||||
onDelete,
|
||||
onList,
|
||||
onUpdate
|
||||
}: {
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
onCreate?: UserCreateRequest;
|
||||
onDelete?: UserDeleteRequest;
|
||||
onList?: UserListRequest;
|
||||
onUpdate?: UserUpdateRequest;
|
||||
} = $props();
|
||||
|
||||
let users = $state<ManagedUser[]>([]);
|
||||
let loading = $state(false);
|
||||
let saving = $state(false);
|
||||
let error = $state('');
|
||||
let notice = $state('');
|
||||
let loaded = $state(false);
|
||||
let newUser = $state<UserCreateInput>({ username: '', email: '', password: '', role: 'user' });
|
||||
let editingId = $state<UserId | null>(null);
|
||||
let editUser = $state<UserUpdateInput>({});
|
||||
|
||||
const canManage = $derived(currentRole === 'admin' || currentRole === 'superadmin');
|
||||
const canAssignAdmin = $derived(currentRole === 'superadmin');
|
||||
|
||||
function roleForForm(role: UserRole | undefined): UserRole {
|
||||
return canAssignAdmin && role ? role : 'user';
|
||||
}
|
||||
|
||||
async function loadUsers(): Promise<void> {
|
||||
if (!onList || !canManage) return;
|
||||
loading = true;
|
||||
error = '';
|
||||
try {
|
||||
users = await onList();
|
||||
loaded = true;
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to load users.';
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (canManage && onList && !loaded) void loadUsers();
|
||||
});
|
||||
|
||||
async function createUser(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
if (!onCreate) return;
|
||||
saving = true;
|
||||
error = '';
|
||||
notice = '';
|
||||
try {
|
||||
await onCreate({ ...newUser, role: roleForForm(newUser.role) });
|
||||
newUser = { username: '', email: '', password: '', role: 'user' };
|
||||
notice = 'User created.';
|
||||
await loadUsers();
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to create user.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
function startEditing(user: ManagedUser): void {
|
||||
editingId = user.id;
|
||||
editUser = { username: user.username, email: user.email, role: roleForForm(user.role) };
|
||||
error = '';
|
||||
notice = '';
|
||||
}
|
||||
|
||||
function stopEditing(): void {
|
||||
editingId = null;
|
||||
editUser = {};
|
||||
}
|
||||
|
||||
async function updateUser(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
if (!onUpdate || editingId === null) return;
|
||||
saving = true;
|
||||
error = '';
|
||||
notice = '';
|
||||
try {
|
||||
const input = { ...editUser, role: roleForForm(editUser.role) };
|
||||
if (!input.password) delete input.password;
|
||||
await onUpdate(editingId, input);
|
||||
stopEditing();
|
||||
notice = 'User updated.';
|
||||
await loadUsers();
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to update user.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteUser(user: ManagedUser): Promise<void> {
|
||||
if (!onDelete || String(user.id) === String(currentUserId)) return;
|
||||
if (!window.confirm(`Delete user ${user.username}?`)) return;
|
||||
saving = true;
|
||||
error = '';
|
||||
notice = '';
|
||||
try {
|
||||
await onDelete(user.id);
|
||||
notice = 'User deleted.';
|
||||
await loadUsers();
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to delete user.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if canManage && onList && onCreate && onUpdate && onDelete}
|
||||
<section class="sc-user-management" aria-labelledby="sc-user-management-title">
|
||||
<div class="sc-user-management-header">
|
||||
<div>
|
||||
<h3 id="sc-user-management-title">User management</h3>
|
||||
<p class="sc-muted">Add, edit, or remove accounts for this project.</p>
|
||||
</div>
|
||||
<Button disabled={loading || saving} size="sm" type="button" variant="secondary" onclick={() => void loadUsers()}>
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<form class="sc-user-form" onsubmit={createUser}>
|
||||
<strong>Add user</strong>
|
||||
<div class="sc-user-grid">
|
||||
<input aria-label="New username" class="sc-input" placeholder="Username" required type="text" bind:value={newUser.username} />
|
||||
<input aria-label="New email" class="sc-input" placeholder="Email" required type="email" bind:value={newUser.email} />
|
||||
<input aria-label="New password" class="sc-input" minlength="8" placeholder="Password" required type="password" bind:value={newUser.password} />
|
||||
<select aria-label="New role" class="sc-input" disabled={!canAssignAdmin} bind:value={newUser.role}>
|
||||
<option value="user">User</option>
|
||||
{#if canAssignAdmin}<option value="admin">Admin</option>{/if}
|
||||
</select>
|
||||
</div>
|
||||
<div class="sc-form-actions"><Button disabled={saving} type="submit">Add user</Button></div>
|
||||
</form>
|
||||
|
||||
{#if error}<p aria-live="polite" class="sc-error">{error}</p>{/if}
|
||||
{#if notice}<p aria-live="polite" class="sc-success">{notice}</p>{/if}
|
||||
|
||||
{#if loading && users.length === 0}
|
||||
<p class="sc-muted">Loading users...</p>
|
||||
{:else if users.length === 0}
|
||||
<p class="sc-muted">No users found.</p>
|
||||
{:else}
|
||||
<div class="sc-user-list">
|
||||
{#each users as user (user.id)}
|
||||
{#if String(editingId) === String(user.id)}
|
||||
<form class="sc-user-row sc-user-edit-row" onsubmit={updateUser}>
|
||||
<input aria-label="Username" class="sc-input" required type="text" bind:value={editUser.username} />
|
||||
<input aria-label="Email" class="sc-input" required type="email" bind:value={editUser.email} />
|
||||
<input aria-label="New password" class="sc-input" minlength="8" placeholder="Keep password" type="password" bind:value={editUser.password} />
|
||||
<select aria-label="Role" class="sc-input" disabled={!canAssignAdmin || user.role === 'superadmin'} bind:value={editUser.role}>
|
||||
<option value="user">User</option>
|
||||
{#if canAssignAdmin}<option value="admin">Admin</option>{/if}
|
||||
{#if user.role === 'superadmin'}<option value="superadmin">Superadmin</option>{/if}
|
||||
</select>
|
||||
<div class="sc-user-actions"><Button disabled={saving} size="sm" type="submit">Save</Button><Button disabled={saving} size="sm" type="button" variant="secondary" onclick={stopEditing}>Cancel</Button></div>
|
||||
</form>
|
||||
{:else}
|
||||
<div class="sc-user-row">
|
||||
<div><strong>{user.username}</strong><span>{user.email}</span></div>
|
||||
<span class="sc-user-role">{user.role}</span>
|
||||
<div class="sc-user-actions">
|
||||
<Button disabled={saving} size="sm" type="button" variant="secondary" onclick={() => startEditing(user)}>Edit</Button>
|
||||
<Button disabled={saving || String(user.id) === String(currentUserId) || user.role === 'superadmin'} size="sm" type="button" variant="danger" onclick={() => void deleteUser(user)}>Remove</Button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</section>
|
||||
{/if}
|
||||
@@ -9,12 +9,29 @@ export { default as Header } from './components/Header.svelte';
|
||||
export { default as PageHeader } from './components/PageHeader.svelte';
|
||||
export { default as ProfileDialog } from './components/ProfileDialog.svelte';
|
||||
export { default as SettingsDialog } from './components/SettingsDialog.svelte';
|
||||
export { default as TotpSettings } from './components/TotpSettings.svelte';
|
||||
export { default as UserManagement } from './components/UserManagement.svelte';
|
||||
export type {
|
||||
AboutLink,
|
||||
AsyncAction,
|
||||
AuthLoginInput,
|
||||
AuthResetConfirm,
|
||||
AuthResetRequest,
|
||||
AuthSetupInput,
|
||||
ContentSnippet,
|
||||
NavItem,
|
||||
ProfileUpdateInput
|
||||
ProfileUpdateInput,
|
||||
ManagedUser,
|
||||
UserCreateInput,
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateInput,
|
||||
UserUpdateRequest,
|
||||
TotpSetupRequest,
|
||||
TotpSetupResponse,
|
||||
TotpStatusRequest,
|
||||
TotpTokenAction
|
||||
} from './types';
|
||||
@@ -330,6 +330,71 @@
|
||||
font-weight: 650;
|
||||
}
|
||||
|
||||
.sc-label-optional {
|
||||
color: var(--sc-muted-fg);
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.sc-totp-settings {
|
||||
display: grid;
|
||||
gap: 0.75rem;
|
||||
margin-top: 1.5rem;
|
||||
padding-top: 1.25rem;
|
||||
border-top: 1px solid var(--sc-border);
|
||||
}
|
||||
|
||||
.sc-totp-heading {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.sc-totp-heading h3 {
|
||||
margin: 0;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.sc-totp-heading p {
|
||||
margin: 0.25rem 0 0;
|
||||
}
|
||||
|
||||
.sc-totp-status {
|
||||
flex: 0 0 auto;
|
||||
border: 1px solid var(--sc-border);
|
||||
border-radius: 999px;
|
||||
padding: 0.2rem 0.55rem;
|
||||
color: var(--sc-muted-fg);
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.sc-totp-status.sc-totp-enabled {
|
||||
border-color: var(--sc-success);
|
||||
color: var(--sc-success);
|
||||
}
|
||||
|
||||
.sc-totp-secret {
|
||||
display: grid;
|
||||
gap: 0.5rem;
|
||||
color: var(--sc-muted-fg);
|
||||
font-size: 0.8rem;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.sc-totp-secret p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.sc-totp-secret code {
|
||||
display: block;
|
||||
border-radius: 0.4rem;
|
||||
background: var(--sc-input-bg);
|
||||
padding: 0.6rem;
|
||||
color: var(--sc-fg);
|
||||
font-size: 0.7rem;
|
||||
}
|
||||
|
||||
.sc-input,
|
||||
.sc-select,
|
||||
.sc-textarea {
|
||||
@@ -383,6 +448,109 @@
|
||||
color: var(--sc-success);
|
||||
}
|
||||
|
||||
.sc-user-management {
|
||||
display: grid;
|
||||
gap: 1rem;
|
||||
margin-top: 1.5rem;
|
||||
padding-top: 1.5rem;
|
||||
border-top: 1px solid var(--sc-border);
|
||||
}
|
||||
|
||||
.sc-user-management-header,
|
||||
.sc-user-row,
|
||||
.sc-user-edit-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.sc-user-management-header {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.sc-user-management-header h3 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.sc-user-management-header p {
|
||||
margin: 0.25rem 0 0;
|
||||
}
|
||||
|
||||
.sc-user-form,
|
||||
.sc-user-list {
|
||||
display: grid;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.sc-user-form {
|
||||
padding: 0.9rem;
|
||||
border: 1px solid var(--sc-border);
|
||||
border-radius: 0.6rem;
|
||||
background: var(--sc-surface-raised);
|
||||
}
|
||||
|
||||
.sc-user-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 0.6rem;
|
||||
}
|
||||
|
||||
.sc-user-row {
|
||||
min-width: 0;
|
||||
justify-content: space-between;
|
||||
padding: 0.7rem 0;
|
||||
border-bottom: 1px solid var(--sc-border);
|
||||
}
|
||||
|
||||
.sc-user-row > div:first-child {
|
||||
display: grid;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.sc-user-row > div:first-child span {
|
||||
overflow: hidden;
|
||||
color: var(--sc-muted-fg);
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.sc-user-role {
|
||||
color: var(--sc-muted-fg);
|
||||
font-size: 0.8rem;
|
||||
text-transform: capitalize;
|
||||
}
|
||||
|
||||
.sc-user-actions {
|
||||
display: flex;
|
||||
flex-shrink: 0;
|
||||
gap: 0.4rem;
|
||||
}
|
||||
|
||||
.sc-user-edit-row {
|
||||
align-items: stretch;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.sc-user-edit-row .sc-input {
|
||||
flex: 1 1 10rem;
|
||||
min-width: 8rem;
|
||||
}
|
||||
|
||||
@media (max-width: 620px) {
|
||||
.sc-user-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.sc-user-row {
|
||||
align-items: flex-start;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.sc-user-actions {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.sc-auth-page {
|
||||
display: grid;
|
||||
min-height: 100vh;
|
||||
@@ -423,6 +591,37 @@
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.sc-auth-reset {
|
||||
display: grid;
|
||||
gap: 0.625rem;
|
||||
margin-top: 1.25rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid var(--sc-border);
|
||||
}
|
||||
|
||||
.sc-auth-reset-hint {
|
||||
color: var(--sc-subtle-fg);
|
||||
font-size: 0.75rem;
|
||||
line-height: 1.45;
|
||||
}
|
||||
|
||||
.sc-auth-reset-link {
|
||||
display: block;
|
||||
margin: 1.25rem auto 0;
|
||||
padding: 0;
|
||||
color: var(--sc-subtle-fg);
|
||||
font: inherit;
|
||||
font-size: 0.75rem;
|
||||
text-decoration: underline;
|
||||
background: none;
|
||||
border: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.sc-auth-reset-link:hover {
|
||||
color: var(--sc-fg);
|
||||
}
|
||||
|
||||
.sc-dialog-backdrop {
|
||||
position: fixed;
|
||||
z-index: 100;
|
||||
|
||||
@@ -16,14 +16,67 @@ export interface AuthSetupInput {
|
||||
export interface AuthLoginInput {
|
||||
identifier: string;
|
||||
password: string;
|
||||
totpToken?: string;
|
||||
}
|
||||
|
||||
export interface TotpSetupResponse {
|
||||
secret: string;
|
||||
otpauthUrl: string;
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export type TotpStatusRequest = () => boolean | Promise<boolean>;
|
||||
|
||||
export type TotpSetupRequest = () => TotpSetupResponse | Promise<TotpSetupResponse>;
|
||||
|
||||
export type TotpTokenAction = (token: string) => void | Promise<void>;
|
||||
|
||||
export type AuthResetRequest = () => void | Promise<unknown>;
|
||||
|
||||
export type AuthResetConfirm = (code: string) => void | Promise<unknown>;
|
||||
|
||||
export interface ProfileUpdateInput {
|
||||
username: string;
|
||||
email: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
export type UserRole = 'superadmin' | 'admin' | 'user';
|
||||
|
||||
export type UserId = string | number;
|
||||
|
||||
export interface ManagedUser {
|
||||
id: UserId;
|
||||
username: string;
|
||||
email: string;
|
||||
role: UserRole;
|
||||
totpEnabled?: boolean;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface UserCreateInput {
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
role?: UserRole;
|
||||
}
|
||||
|
||||
export interface UserUpdateInput {
|
||||
username?: string;
|
||||
email?: string;
|
||||
password?: string;
|
||||
role?: UserRole;
|
||||
}
|
||||
|
||||
export type UserListRequest = () => ManagedUser[] | Promise<ManagedUser[]>;
|
||||
|
||||
export type UserCreateRequest = (input: UserCreateInput) => void | Promise<void | ManagedUser>;
|
||||
|
||||
export type UserUpdateRequest = (id: UserId, input: UserUpdateInput) => void | Promise<void | ManagedUser>;
|
||||
|
||||
export type UserDeleteRequest = (id: UserId) => void | Promise<void>;
|
||||
|
||||
export interface AboutLink {
|
||||
label: string;
|
||||
href: string;
|
||||
|
||||
Reference in new issue
Block a user