mirror of
https://github.com/thegeneralist01/archivr
synced 2026-12-04 18:56:44 +01:00
65 lines
2 KiB
JavaScript
65 lines
2 KiB
JavaScript
import { useState } from 'react';
|
|
import { login } from '../api.js';
|
|
import ThemeToggle from './ThemeToggle.jsx';
|
|
|
|
export default function LoginPage({ onLogin }) {
|
|
const [username, setUsername] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
async function handleSubmit(e) {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setLoading(true);
|
|
try {
|
|
const user = await login(username, password);
|
|
onLogin(user);
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="login-page">
|
|
<ThemeToggle className="auth-page-theme-toggle" />
|
|
<div className="login-card">
|
|
<h1 className="login-brand">Archivr</h1>
|
|
<p className="login-tagline">Sign in to your archive</p>
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="login-field">
|
|
<label className="login-label" htmlFor="login-username">Username</label>
|
|
<input
|
|
className="login-input"
|
|
id="login-username"
|
|
type="text"
|
|
value={username}
|
|
onChange={e => setUsername(e.target.value)}
|
|
autoFocus
|
|
required
|
|
autoComplete="username"
|
|
/>
|
|
</div>
|
|
<div className="login-field">
|
|
<label className="login-label" htmlFor="login-password">Password</label>
|
|
<input
|
|
className="login-input"
|
|
id="login-password"
|
|
type="password"
|
|
value={password}
|
|
onChange={e => setPassword(e.target.value)}
|
|
required
|
|
autoComplete="current-password"
|
|
/>
|
|
</div>
|
|
{error && <p className="login-error">{error}</p>}
|
|
<button className="login-submit" type="submit" disabled={loading}>
|
|
{loading ? 'Signing in\u2026' : 'Sign in'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|