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>
64 lines
1.8 KiB
TypeScript
64 lines
1.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';
|
|
|
|
export function LoginPage() {
|
|
const { login, user, needsSetup } = useAuth();
|
|
const [username, setUsername] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
if (needsSetup) return <Navigate to="/setup" replace />;
|
|
if (user) return <Navigate to="/" replace />;
|
|
|
|
async function handleSubmit(e: FormEvent) {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setSubmitting(true);
|
|
try {
|
|
await login(username, password);
|
|
} catch (err) {
|
|
setError(
|
|
err instanceof ApiError && err.status === 401
|
|
? 'Invalid username or password.'
|
|
: 'Something went wrong. Please try again.',
|
|
);
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="auth-page">
|
|
<form className="auth-form" onSubmit={handleSubmit}>
|
|
<h1>CityTracker</h1>
|
|
<label>
|
|
Username
|
|
<input
|
|
value={username}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
autoComplete="username"
|
|
required
|
|
autoFocus
|
|
/>
|
|
</label>
|
|
<label>
|
|
Password
|
|
<input
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
autoComplete="current-password"
|
|
required
|
|
/>
|
|
</label>
|
|
{error && <p className="form-error">{error}</p>}
|
|
<button type="submit" className="btn btn-primary" disabled={submitting}>
|
|
{submitting ? 'Logging in…' : 'Log in'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|