✨ Add login and sign-up page

The app no longer uses a shared bearer token. The page signs users in
or creates an account through PocketBase and offers the configured
OAuth2 providers.
This commit is contained in:
2026-11-30 17:43:13 +00:00
parent 6a95b885b8
commit 23cecdea80
+253
View File
@@ -0,0 +1,253 @@
<script lang="ts">
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { page } from '$app/state';
import type { AuthProviderInfo } from 'pocketbase';
import { errorMessage, pb } from '$lib/pb';
import { login, resolveGate, safeNext, signup } from '$lib/session.svelte';
let mode = $state<'signin' | 'signup'>('signin');
let email = $state('');
let password = $state('');
let confirmation = $state('');
let error = $state('');
let busy = $state(false);
let providers = $state<AuthProviderInfo[]>([]);
const signingUp = $derived(mode === 'signup');
onMount(async () => {
try {
const methods = await pb.collection('users').listAuthMethods();
providers = methods.oauth2.enabled ? methods.oauth2.providers : [];
} catch {
providers = [];
}
});
function switchMode() {
mode = signingUp ? 'signin' : 'signup';
error = '';
}
function describe(err: unknown): string {
const data = (err as { response?: { data?: Record<string, { message?: string } | null> } }).response?.data;
const fields = data
? Object.entries(data).map(([field, detail]) => `${field}: ${detail?.message ?? 'invalid'}`).join(', ')
: '';
return fields || errorMessage(err);
}
async function submit(event: SubmitEvent) {
event.preventDefault();
error = '';
if (signingUp && password !== confirmation) {
error = 'Passwords do not match';
return;
}
const next = safeNext(page.url);
busy = true;
try {
if (signingUp) await signup(email, password);
else await login(email, password);
await goto(next, { replaceState: true });
} catch (err) {
error = describe(err);
} finally {
busy = false;
}
}
async function oauth(provider: string) {
error = '';
const next = safeNext(page.url);
busy = true;
try {
await pb.collection('users').authWithOAuth2({ provider });
if ((await resolveGate()) !== 'app') throw new Error('Sign in failed');
await goto(next, { replaceState: true });
} catch (err) {
error = describe(err);
} finally {
busy = false;
}
}
</script>
<div class="card">
<h1>Wotra</h1>
<p class="subtitle">Working time tracker</p>
<form onsubmit={submit}>
<label>
Email
<input type="email" autocomplete="email" bind:value={email} required />
</label>
<label>
Password
<input
type="password"
autocomplete={signingUp ? 'new-password' : 'current-password'}
minlength={signingUp ? 8 : undefined}
bind:value={password}
required
/>
</label>
{#if signingUp}
<label>
Confirm password
<input type="password" autocomplete="new-password" minlength="8" bind:value={confirmation} required />
</label>
{/if}
{#if error}
<p class="error" role="alert">{error}</p>
{/if}
<button type="submit" disabled={busy}>{signingUp ? 'Create account' : 'Sign in'}</button>
</form>
{#if providers.length > 0}
<div class="divider">or</div>
<div class="providers">
{#each providers as provider (provider.name)}
<button type="button" class="oauth" disabled={busy} onclick={() => void oauth(provider.name)}>
Continue with {provider.displayName}
</button>
{/each}
</div>
{/if}
<p class="switch">
{#if signingUp}
Already have an account?
<button type="button" class="link" onclick={switchMode}>Sign in instead</button>
{:else}
New to Wotra?
<button type="button" class="link" onclick={switchMode}>Create an account</button>
{/if}
</p>
</div>
<style>
.card {
width: 100%;
max-width: 360px;
margin: 0 auto;
background: #fff;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 2rem 1.75rem;
box-shadow: 0 4px 16px rgba(26, 26, 46, 0.08);
}
h1 {
margin: 0;
text-align: center;
font-size: 1.75rem;
letter-spacing: 0.04em;
color: #1a1a2e;
}
.subtitle {
margin: 0.25rem 0 1.75rem;
text-align: center;
font-size: 0.9rem;
color: #6c757d;
}
form {
display: flex;
flex-direction: column;
gap: 0.9rem;
}
label {
display: flex;
flex-direction: column;
gap: 0.3rem;
font-size: 0.9rem;
font-weight: 500;
color: #495057;
}
input {
padding: 0.5rem 0.75rem;
border: 1px solid #ced4da;
border-radius: 6px;
font-size: 0.95rem;
font-weight: 400;
}
input:focus {
outline: 2px solid rgba(26, 26, 46, 0.2);
border-color: #1a1a2e;
}
button {
cursor: pointer;
font: inherit;
}
button:disabled {
opacity: 0.55;
cursor: not-allowed;
}
button[type="submit"] {
margin-top: 0.25rem;
padding: 0.55rem 1.4rem;
background: #1a1a2e;
color: #fff;
border: none;
border-radius: 6px;
font-size: 0.95rem;
font-weight: 600;
}
button[type="submit"]:hover:not(:disabled) {
background: #16213e;
}
.error {
margin: 0;
padding: 0.5rem 0.75rem;
border-radius: 6px;
background: #fde8e4;
color: #c0392b;
font-size: 0.875rem;
}
.divider {
display: flex;
align-items: center;
gap: 0.75rem;
margin: 1.25rem 0 0.9rem;
color: #6c757d;
font-size: 0.8rem;
}
.divider::before,
.divider::after {
content: '';
flex: 1;
height: 1px;
background: #dee2e6;
}
.providers {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.oauth {
padding: 0.5rem 1rem;
background: #fff;
border: 1px solid #ced4da;
border-radius: 6px;
color: #343a40;
font-size: 0.9rem;
font-weight: 500;
}
.oauth:hover:not(:disabled) {
border-color: #1a1a2e;
}
.switch {
margin: 1.5rem 0 0;
text-align: center;
font-size: 0.875rem;
color: #6c757d;
}
.link {
background: none;
border: none;
padding: 0;
color: #1a1a2e;
font-weight: 600;
text-decoration: underline;
}
</style>