feat(auth): enhance authentication flow with TOTP and admin reset features
Docker / Docker Build & Push (push) Failing after 56s
Lint / Lint (push) Successful in 25s
Tests / Go backend (push) Failing after 19s
Tests / Dashboard checks (push) Successful in 24s

- Added TOTP setup, enable, and disable functions to the API.
- Implemented admin reset request and confirmation functionalities.
- Updated AuthPage to handle new reset functionalities and TOTP inputs.
- Enhanced auth store to manage user creation, deletion, and listing.
- Improved AppShell and SettingsDialog to support user management features.
- Refactored API calls for better organization and clarity.
This commit is contained in:
Gabriel20xx committed 2026-08-05 14:58:07 +02:00
1 parent ac08be0248
commit 652e81dacb
9 files changed
+4333 -3737

No files matched your search

@@ -2,17 +2,23 @@
import { AuthPage as SharedAuthPage } from '@gabriel20xx/shared-code/web-ui';
import { useAuth } from '$lib/features/auth/auth.svelte.js';
let { setupRequired } = $props();
let { setupRequired, loading = false, error = '' } = $props();
const auth = useAuth();
const onLogin = (input) => auth.login(input.identifier, input.password);
const onLogin = (input) => auth.login(input.identifier, input.password, input.totpToken);
const onSetup = (input) => auth.setup(input.username, input.email, input.password);
const onRequestReset = () => auth.requestAdminReset();
const onConfirmReset = (code) => auth.confirmAdminReset(code);
</script>
<SharedAuthPage
brand="CollabTable"
tagline="Collaborative tables for your team"
{setupRequired}
{loading}
{error}
{onLogin}
{onSetup}
{onRequestReset}
{onConfirmReset}
/>
+101 -52
View File
@@ -1,68 +1,117 @@
import { createMutation, createQuery, useQueryClient } from '@tanstack/svelte-query';
import { getContext, setContext } from 'svelte';
import { fetchAuthStatus, login, logout, setupAccount, updateCredentials } from '$lib/api.js';
const AUTH_STATUS_KEY = ['AuthStatus'];
const AUTH_CONTEXT_KEY = Symbol('auth');
export function createAuthStore() {
const queryClient = useQueryClient();
const statusQuery = createQuery(() => ({
queryKey: AUTH_STATUS_KEY,
queryFn: fetchAuthStatus,
}));
import { createMutation, createQuery, useQueryClient } from '@tanstack/svelte-query';
import { getContext, setContext } from 'svelte';
import {
confirmAdminReset,
createUser,
deleteUser,
disableTotp,
enableTotp,
fetchAuthStatus,
fetchTotpStatus,
login,
listUsers,
logout,
requestAdminReset,
setupTotp,
setupAccount,
updateCredentials,
updateUser,
} from '$lib/api.js';
const AUTH_STATUS_KEY = ['AuthStatus'];
const AUTH_CONTEXT_KEY = Symbol('auth');
export function createAuthStore() {
const queryClient = useQueryClient();
const statusQuery = createQuery(() => ({
queryKey: AUTH_STATUS_KEY,
queryFn: fetchAuthStatus,
}));
const setupMutation = createMutation(() => ({
mutationFn: ({ username, email, password }) => setupAccount(username, email, password),
}));
const loginMutation = createMutation(() => ({
mutationFn: ({ identifier, password }) => login(identifier, password),
}));
const logoutMutation = createMutation(() => ({
mutationFn: () => logout(),
}));
const updateCredentialsMutation = createMutation(() => ({
mutationFn: (input) => updateCredentials(input),
}));
const refresh = () => queryClient.invalidateQueries({ queryKey: AUTH_STATUS_KEY });
const store = {
get status() {
return statusQuery.data;
},
get loading() {
return statusQuery.isLoading;
},
get error() {
return statusQuery.error;
},
mutationFn: ({ identifier, password, totpToken }) => login(identifier, password, totpToken),
}));
const logoutMutation = createMutation(() => ({
mutationFn: () => logout(),
}));
const updateCredentialsMutation = createMutation(() => ({
mutationFn: (input) => updateCredentials(input),
}));
const refresh = () => queryClient.invalidateQueries({ queryKey: AUTH_STATUS_KEY });
const store = {
get status() {
return statusQuery.data;
},
get loading() {
return statusQuery.isLoading;
},
get error() {
return statusQuery.error;
},
async setup(username, email, password) {
await setupMutation.mutateAsync({ username, email, password });
await refresh();
},
async login(identifier, password, totpToken = '') {
await loginMutation.mutateAsync({ identifier, password, totpToken });
await refresh();
},
async login(identifier, password) {
await loginMutation.mutateAsync({ identifier, password });
async totpStatus() {
const result = await fetchTotpStatus();
return result?.enabled === true;
},
async setupTotp() {
return setupTotp();
},
async enableTotp(token) {
await enableTotp(token);
await refresh();
},
async logout() {
await logoutMutation.mutateAsync();
async disableTotp(token) {
await disableTotp(token);
await refresh();
},
async requestAdminReset() {
await requestAdminReset();
},
async confirmAdminReset(code) {
await confirmAdminReset(code);
},
async logout() {
await logoutMutation.mutateAsync();
await refresh();
},
async updateCredentials(input) {
await updateCredentialsMutation.mutateAsync(input);
await refresh();
},
};
setContext(AUTH_CONTEXT_KEY, store);
return store;
}
export function useAuth() {
const ctx = getContext(AUTH_CONTEXT_KEY);
if (!ctx) {
throw new Error('useAuth() must be called within the root layout that calls createAuthStore()');
}
return ctx;
}
async listUsers() {
return listUsers();
},
async createUser(input) {
await createUser(input);
},
async updateUser(id, input) {
await updateUser(id, input);
},
async deleteUser(id) {
await deleteUser(id);
},
};
setContext(AUTH_CONTEXT_KEY, store);
return store;
}
export function useAuth() {
const ctx = getContext(AUTH_CONTEXT_KEY);
if (!ctx) {
throw new Error('useAuth() must be called within the root layout that calls createAuthStore()');
}
return ctx;
}
@@ -26,11 +26,12 @@
<Tooltip.Provider delayDuration={300}>
{#if auth.loading}
<div class="flex min-h-screen items-center justify-center text-sm text-muted-fg">Loading…</div>
<AuthPage setupRequired={auth.status?.setupRequired ?? true} loading />
{:else if auth.error}
<div class="flex min-h-screen items-center justify-center px-4 text-sm text-danger">
{auth.error instanceof Error ? auth.error.message : 'Request failed.'}
</div>
<AuthPage
setupRequired={auth.status?.setupRequired ?? true}
error={auth.error instanceof Error ? auth.error.message : 'Request failed.'}
/>
{:else if !auth.status || auth.status.setupRequired || !auth.status.authenticated}
<AuthPage setupRequired={auth.status?.setupRequired ?? true} />
{:else}
@@ -44,17 +45,33 @@
onProfile={() => (credentialsOpen = true)}
onSettings={() => (settingsOpen = true)}
>
{#snippet children()}
{@render children?.()}
{/snippet}
{@render children?.()}
</SharedAppShell>
{/if}
</Tooltip.Provider>
<SettingsDialog bind:open={settingsOpen} />
<SettingsDialog
bind:open={settingsOpen}
currentRole={auth.status?.role ?? 'user'}
currentUserId={auth.status?.id}
onCreateUser={(input) => auth.createUser(input)}
onDeleteUser={(id) => auth.deleteUser(id)}
onListUsers={() => auth.listUsers()}
onUpdateUser={(id, input) => auth.updateUser(id, input)}
/>
<ProfileDialog
bind:open={credentialsOpen}
currentUsername={auth.status?.username ?? ''}
currentEmail={auth.status?.email ?? ''}
currentRole={auth.status?.role ?? 'user'}
currentUserId={auth.status?.id}
onCreateUser={(input) => auth.createUser(input)}
onDeleteUser={(id) => auth.deleteUser(id)}
onListUsers={() => auth.listUsers()}
onSave={saveProfile}
onUpdateUser={(id, input) => auth.updateUser(id, input)}
onTotpStatus={() => auth.totpStatus()}
onTotpSetup={() => auth.setupTotp()}
onTotpEnable={(token) => auth.enableTotp(token)}
onTotpDisable={(token) => auth.disableTotp(token)}
/>
@@ -1,4 +1,4 @@
<script>
<script>
import { Moon, Sun, SunMoon } from '@lucide/svelte';
import Button from '$lib/components/ui/button.svelte';
import { SettingsDialog as SharedSettingsDialog } from '@gabriel20xx/shared-code/web-ui';
@@ -6,45 +6,62 @@
import { currentLanguage, languages, setLanguage } from '$lib/language.svelte.js';
import { themeState } from '$lib/theme.svelte.js';
let { open = $bindable(false) } = $props();
let {
currentRole = 'user',
currentUserId,
onCreateUser,
onDeleteUser,
onListUsers,
onUpdateUser,
open = $bindable(false)
} = $props();
const languageItems = languages.map((language) => ({ value: language.id, label: language.label }));
</script>
<SharedSettingsDialog bind:open title="Settings">
</script>
<SharedSettingsDialog
bind:open
currentRole={currentRole}
currentUserId={currentUserId}
onCreateUser={onCreateUser}
onDeleteUser={onDeleteUser}
onListUsers={onListUsers}
onUpdateUser={onUpdateUser}
title="Settings"
>
<div>
<span class="mb-2 block text-sm font-medium">Theme</span>
<div class="flex w-full rounded-md border border-border p-1">
<Button
type="button"
variant={themeState.mode === 'light' ? 'secondary' : 'ghost'}
size="sm"
class="flex-1 gap-1.5"
onclick={() => themeState.setMode('light')}
>
<Sun class="size-4" />
Light
</Button>
<Button
type="button"
variant={themeState.mode === 'dark' ? 'secondary' : 'ghost'}
size="sm"
class="flex-1 gap-1.5"
onclick={() => themeState.setMode('dark')}
>
<Moon class="size-4" />
Dark
</Button>
<Button
type="button"
variant={themeState.mode === 'system' ? 'secondary' : 'ghost'}
size="sm"
class="flex-1 gap-1.5"
onclick={() => themeState.setMode('system')}
>
<SunMoon class="size-4" />
System
</Button>
<div class="flex w-full rounded-md border border-border p-1">
<Button
type="button"
variant={themeState.mode === 'light' ? 'secondary' : 'ghost'}
size="sm"
class="flex-1 gap-1.5"
onclick={() => themeState.setMode('light')}
>
<Sun class="size-4" />
Light
</Button>
<Button
type="button"
variant={themeState.mode === 'dark' ? 'secondary' : 'ghost'}
size="sm"
class="flex-1 gap-1.5"
onclick={() => themeState.setMode('dark')}
>
<Moon class="size-4" />
Dark
</Button>
<Button
type="button"
variant={themeState.mode === 'system' ? 'secondary' : 'ghost'}
size="sm"
class="flex-1 gap-1.5"
onclick={() => themeState.setMode('system')}
>
<SunMoon class="size-4" />
System
</Button>
</div>
</div>
<div class="mt-5">