Spaces:
Running
Running
Commit ·
59d41c0
1
Parent(s): 4bb4f90
feat(ui): add layout, tab bar, login and logout
Browse files- src/lib/components/FormError.svelte +7 -0
- src/lib/components/MoneyText.svelte +19 -0
- src/lib/components/TabBar.svelte +38 -0
- src/routes/+layout.server.ts +8 -0
- src/routes/+layout.svelte +24 -0
- src/routes/login/+page.server.ts +34 -0
- src/routes/login/+page.svelte +32 -0
- src/routes/logout/+server.ts +7 -0
src/lib/components/FormError.svelte
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
let { message }: { message?: string } = $props();
|
| 3 |
+
</script>
|
| 4 |
+
|
| 5 |
+
{#if message}
|
| 6 |
+
<p class="mt-1 text-sm text-red-700" role="alert">{message}</p>
|
| 7 |
+
{/if}
|
src/lib/components/MoneyText.svelte
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { formatCents } from '$lib/domain/money';
|
| 3 |
+
|
| 4 |
+
let {
|
| 5 |
+
cents,
|
| 6 |
+
currency,
|
| 7 |
+
signed = false,
|
| 8 |
+
class: className = ''
|
| 9 |
+
}: { cents: number; currency: string; signed?: boolean; class?: string } = $props();
|
| 10 |
+
|
| 11 |
+
const text = $derived(
|
| 12 |
+
signed && cents > 0 ? `+${formatCents(cents, currency)}` : formatCents(cents, currency)
|
| 13 |
+
);
|
| 14 |
+
const tone = $derived(
|
| 15 |
+
!signed ? '' : cents > 0 ? 'text-green-700' : cents < 0 ? 'text-red-700' : 'text-gray-500'
|
| 16 |
+
);
|
| 17 |
+
</script>
|
| 18 |
+
|
| 19 |
+
<span class="tabular-nums {tone} {className}">{text}</span>
|
src/lib/components/TabBar.svelte
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { page } from '$app/state';
|
| 3 |
+
import type { PublicUser } from '$lib/domain/types';
|
| 4 |
+
|
| 5 |
+
let { user }: { user: PublicUser } = $props();
|
| 6 |
+
|
| 7 |
+
const tabs = $derived([
|
| 8 |
+
{ href: '/', label: 'Expenses', icon: '🧾' },
|
| 9 |
+
{ href: '/balances', label: 'Balances', icon: '⚖️' },
|
| 10 |
+
{ href: '/expenses/new', label: 'Add', icon: '➕' },
|
| 11 |
+
...(user.role === 'admin' ? [{ href: '/admin', label: 'Admin', icon: '🛠️' }] : []),
|
| 12 |
+
{ href: '/me', label: 'Me', icon: '👤' }
|
| 13 |
+
]);
|
| 14 |
+
|
| 15 |
+
const isActive = (href: string) =>
|
| 16 |
+
href === '/' ? page.url.pathname === '/' : page.url.pathname.startsWith(href);
|
| 17 |
+
</script>
|
| 18 |
+
|
| 19 |
+
<nav
|
| 20 |
+
class="fixed inset-x-0 bottom-0 z-10 border-t border-gray-200 bg-white pb-[env(safe-area-inset-bottom)]"
|
| 21 |
+
>
|
| 22 |
+
<ul class="mx-auto flex max-w-lg justify-around">
|
| 23 |
+
{#each tabs as tab (tab.href)}
|
| 24 |
+
<li class="flex-1">
|
| 25 |
+
<a
|
| 26 |
+
href={tab.href}
|
| 27 |
+
class="flex flex-col items-center py-2 text-xs {isActive(tab.href)
|
| 28 |
+
? 'font-semibold text-green-700'
|
| 29 |
+
: 'text-gray-500'}"
|
| 30 |
+
aria-current={isActive(tab.href) ? 'page' : undefined}
|
| 31 |
+
>
|
| 32 |
+
<span class="text-lg" aria-hidden="true">{tab.icon}</span>
|
| 33 |
+
{tab.label}
|
| 34 |
+
</a>
|
| 35 |
+
</li>
|
| 36 |
+
{/each}
|
| 37 |
+
</ul>
|
| 38 |
+
</nav>
|
src/routes/+layout.server.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { toPublicUser } from '$lib/domain/types';
|
| 2 |
+
import type { LayoutServerLoad } from './$types';
|
| 3 |
+
|
| 4 |
+
export const load: LayoutServerLoad = ({ locals }) => ({
|
| 5 |
+
user: locals.user ? toPublicUser(locals.user) : null,
|
| 6 |
+
currency: locals.app.config.currency,
|
| 7 |
+
readOnly: !locals.app.guard.canWrite
|
| 8 |
+
});
|
src/routes/+layout.svelte
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import '../app.css';
|
| 3 |
+
import TabBar from '$lib/components/TabBar.svelte';
|
| 4 |
+
import type { LayoutProps } from './$types';
|
| 5 |
+
|
| 6 |
+
let { data, children }: LayoutProps = $props();
|
| 7 |
+
</script>
|
| 8 |
+
|
| 9 |
+
<svelte:head>
|
| 10 |
+
<title>Splitwise</title>
|
| 11 |
+
</svelte:head>
|
| 12 |
+
|
| 13 |
+
<div class="mx-auto min-h-dvh w-full max-w-lg pb-24">
|
| 14 |
+
{#if data.user && data.readOnly}
|
| 15 |
+
<div class="bg-amber-100 px-4 py-2 text-sm text-amber-900">
|
| 16 |
+
Read-only mode: another instance of the app is active. Changes are disabled for a few minutes.
|
| 17 |
+
</div>
|
| 18 |
+
{/if}
|
| 19 |
+
{@render children()}
|
| 20 |
+
</div>
|
| 21 |
+
|
| 22 |
+
{#if data.user}
|
| 23 |
+
<TabBar user={data.user} />
|
| 24 |
+
{/if}
|
src/routes/login/+page.server.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { dev } from '$app/environment';
|
| 2 |
+
import { createSessionToken, SESSION_COOKIE, SESSION_TTL_SECONDS } from '$lib/server/auth/session';
|
| 3 |
+
import { fail, redirect } from '@sveltejs/kit';
|
| 4 |
+
import type { Actions } from './$types';
|
| 5 |
+
|
| 6 |
+
export const actions: Actions = {
|
| 7 |
+
default: async ({ request, locals, cookies, url }) => {
|
| 8 |
+
const form = await request.formData();
|
| 9 |
+
const username = String(form.get('username') ?? '').trim();
|
| 10 |
+
const password = String(form.get('password') ?? '');
|
| 11 |
+
if (!username || !password)
|
| 12 |
+
return fail(400, { error: 'Enter your username and password', username });
|
| 13 |
+
if (!locals.app.loginLimiter.allow(username.toLowerCase())) {
|
| 14 |
+
return fail(429, { error: 'Too many attempts. Wait a minute and try again.', username });
|
| 15 |
+
}
|
| 16 |
+
const user = await locals.app.users.authenticate(username, password);
|
| 17 |
+
if (!user) return fail(400, { error: 'Invalid username or password', username });
|
| 18 |
+
|
| 19 |
+
const token = createSessionToken(
|
| 20 |
+
{ uid: user.id, sv: user.sessionVersion },
|
| 21 |
+
locals.app.config.sessionSecret
|
| 22 |
+
);
|
| 23 |
+
cookies.set(SESSION_COOKIE, token, {
|
| 24 |
+
path: '/',
|
| 25 |
+
httpOnly: true,
|
| 26 |
+
sameSite: 'lax',
|
| 27 |
+
secure: !dev,
|
| 28 |
+
maxAge: SESSION_TTL_SECONDS
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
const next = url.searchParams.get('next');
|
| 32 |
+
redirect(303, next && next.startsWith('/') && !next.startsWith('//') ? next : '/');
|
| 33 |
+
}
|
| 34 |
+
};
|
src/routes/login/+page.svelte
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { enhance } from '$app/forms';
|
| 3 |
+
import FormError from '$lib/components/FormError.svelte';
|
| 4 |
+
import type { PageProps } from './$types';
|
| 5 |
+
|
| 6 |
+
let { form }: PageProps = $props();
|
| 7 |
+
</script>
|
| 8 |
+
|
| 9 |
+
<main class="flex min-h-dvh flex-col justify-center px-6">
|
| 10 |
+
<h1 class="mb-8 text-center text-3xl font-bold text-green-700">Splitwise</h1>
|
| 11 |
+
<form method="POST" use:enhance class="space-y-4">
|
| 12 |
+
<div>
|
| 13 |
+
<label for="username">Username</label>
|
| 14 |
+
<input
|
| 15 |
+
id="username"
|
| 16 |
+
name="username"
|
| 17 |
+
autocomplete="username"
|
| 18 |
+
autocapitalize="none"
|
| 19 |
+
required
|
| 20 |
+
value={form?.username ?? ''}
|
| 21 |
+
/>
|
| 22 |
+
</div>
|
| 23 |
+
<div>
|
| 24 |
+
<label for="password">Password</label>
|
| 25 |
+
<input id="password" name="password" type="password" autocomplete="current-password" required />
|
| 26 |
+
</div>
|
| 27 |
+
<FormError message={form?.error} />
|
| 28 |
+
<button type="submit" class="w-full rounded-lg bg-green-700 py-3 font-semibold text-white"
|
| 29 |
+
>Log in</button
|
| 30 |
+
>
|
| 31 |
+
</form>
|
| 32 |
+
</main>
|
src/routes/logout/+server.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { SESSION_COOKIE } from '$lib/server/auth/session';
|
| 2 |
+
import { redirect, type RequestHandler } from '@sveltejs/kit';
|
| 3 |
+
|
| 4 |
+
export const POST: RequestHandler = ({ cookies }) => {
|
| 5 |
+
cookies.delete(SESSION_COOKIE, { path: '/' });
|
| 6 |
+
redirect(303, '/login');
|
| 7 |
+
};
|