CityTracker/client/src/pages/SetupPage.tsx
Claudio Schaad 3debb1ce1e Add CityTracker v1: map-based city tracker with multi-user support
React/Vite frontend and Express/SQLite backend for tracking visited and
planned cities on a map, with per-city notes, photos, links, and
participant-based sharing between family members. Includes session auth
with admin-managed user accounts, an OSM/Nominatim geocoding proxy, and a
single-image Docker deployment.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 20:29:25 +02:00

88 lines
2.8 KiB
TypeScript

import { useState, type FormEvent } from 'react';
import { Navigate } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
import { ApiError } from '../api/client';
function extractErrorMessage(err: unknown): string {
if (err instanceof ApiError && err.body && typeof err.body === 'object' && 'error' in err.body) {
const code = String((err.body as { error: unknown }).error);
if (code === 'username_taken') return 'That username is already taken.';
if (code === 'setup_already_complete') return 'Setup was already completed. Please log in.';
}
return 'Something went wrong. Please try again.';
}
export function SetupPage() {
const { completeSetup, user, needsSetup } = useAuth();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [error, setError] = useState<string | null>(null);
const [submitting, setSubmitting] = useState(false);
if (user) return <Navigate to="/" replace />;
if (!needsSetup) return <Navigate to="/login" replace />;
async function handleSubmit(e: FormEvent) {
e.preventDefault();
setError(null);
if (password !== confirmPassword) {
setError('Passwords do not match.');
return;
}
setSubmitting(true);
try {
await completeSetup(username, password);
} catch (err) {
setError(extractErrorMessage(err));
} finally {
setSubmitting(false);
}
}
return (
<div className="auth-page">
<form className="auth-form" onSubmit={handleSubmit}>
<h1>Welcome to CityTracker</h1>
<p className="auth-hint">Create the first admin account to get started.</p>
<label>
Username
<input
value={username}
onChange={(e) => setUsername(e.target.value)}
autoComplete="username"
required
minLength={3}
autoFocus
/>
</label>
<label>
Password
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
autoComplete="new-password"
required
minLength={8}
/>
</label>
<label>
Confirm password
<input
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
autoComplete="new-password"
required
minLength={8}
/>
</label>
{error && <p className="form-error">{error}</p>}
<button type="submit" className="btn btn-primary" disabled={submitting}>
{submitting ? 'Creating…' : 'Create admin account'}
</button>
</form>
</div>
);
}