From e913eb34f20a1550d77c366ece161beae516d5ef Mon Sep 17 00:00:00 2001 From: Sami Abuzakuk Date: Sat, 1 Nov 2025 22:21:06 +0100 Subject: [PATCH] Add user frontend support --- frontend/src/lib/api.ts | 43 +++++++++ frontend/src/routes/login/+page.svelte | 2 + frontend/src/routes/settings/+page.svelte | 104 +++++++++++++++++++++- 3 files changed, 147 insertions(+), 2 deletions(-) diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index aa43951..e542c76 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1,4 +1,9 @@ import { env } from '$env/dynamic/public'; +export interface User { + id: number; + username: string; + password: string | null; +} export const API_URL = env.PUBLIC_API_URL || 'http://localhost:8000'; @@ -288,6 +293,44 @@ export async function deleteSubscription(subscriptionId: number): Promise } } +// Fetch all users +export async function fetchUsers(): Promise { + const response = await fetch(`${API_URL}/users`, { + headers: authHeaders() + }); + if (!response.ok) { + throw new Error('Failed to fetch users'); + } + return response.json(); +} + +// Update a user +export async function updateUser( + userId: number, + username: string, + password: string +): Promise { + const response = await fetch(`${API_URL}/users/${userId}`, { + method: 'PUT', + headers: authHeaders({ 'Content-Type': 'application/json' }), + body: JSON.stringify({ username, password }) + }); + if (!response.ok) { + throw new Error('Failed to update user'); + } +} + +// Delete a user +export async function deleteUser(userId: number): Promise { + const response = await fetch(`${API_URL}/users/${userId}`, { + method: 'DELETE', + headers: authHeaders() + }); + if (!response.ok) { + throw new Error('Failed to delete user'); + } +} + // Get subscription notifications with pagination export async function fetchSubscriptionNotifications( subscriptionId: string, diff --git a/frontend/src/routes/login/+page.svelte b/frontend/src/routes/login/+page.svelte index a20958d..bc05b20 100644 --- a/frontend/src/routes/login/+page.svelte +++ b/frontend/src/routes/login/+page.svelte @@ -35,6 +35,8 @@ 'token', JSON.stringify({ value: data.access_token, expiresAt: expirationTime }) ); + localStorage.setItem('username', loginUsername); + goto('/').then(() => location.reload()); } catch (err) { loginError = 'Network error - ' + err; diff --git a/frontend/src/routes/settings/+page.svelte b/frontend/src/routes/settings/+page.svelte index 266b23b..6fe51a4 100644 --- a/frontend/src/routes/settings/+page.svelte +++ b/frontend/src/routes/settings/+page.svelte @@ -1,10 +1,12 @@ @@ -80,6 +121,65 @@ +

User Management

+ {#if isLoading} +

Loading users...

+ {:else if error} +

{$error}

+ {:else} + + + + + + + + + {#each users as user (user.id)} + + + + + {/each} + +
UsersActions
+ {#if user.username === currentUser} + + + + {:else} + {user.username} + {/if} + + {#if user.username === currentUser} + + {:else} + + {/if} +
+ {/if} {/if}