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
@@ -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}
|
||||
Reference in new issue
Block a user