96043efe46
Passkey-Login wie in ausgaben-next/werte-next: auf der Login-Seite "Mit Passkey anmelden", Verwaltung der eigenen Passkeys unter /einstellungen (Button oben rechts). Das Passwort bleibt als Alternative bestehen. - lib/webauthn.ts, lib/passkeys.ts sowie API-Routen unter /api/passkey - proxy.ts: /api/passkey/authenticate ohne Session erreichbar - Styles als eigene CSS-Klassen in globals.css (kein Tailwind in diesem Projekt), passend zum vorhandenen login-*/app-*-Schema - Tabelle tabletten_passkeys in der DB medizin: app-spezifischer Name, damit sich Apps desselben Stacks nicht gegenseitig Passkeys anzeigen oder loeschen - RP-Konfiguration ueber TABLETTEN_RP_*, da sich die .env auf dem Server mit anderen Apps teilt, die RP_ID/RP_ORIGIN bereits belegen Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
164 lines
6.4 KiB
TypeScript
164 lines
6.4 KiB
TypeScript
'use client';
|
|
|
|
import { useActionState, useState } from 'react';
|
|
import { startAuthentication } from '@simplewebauthn/browser';
|
|
import { login } from './actions';
|
|
import packageJson from '@/package.json';
|
|
|
|
export default function LoginPage() {
|
|
const [state, loginAction, isPending] = useActionState(login, undefined);
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
const [passkeyBusy, setPasskeyBusy] = useState(false);
|
|
const [passkeyError, setPasskeyError] = useState<string | null>(null);
|
|
|
|
async function handlePasskeyLogin() {
|
|
setPasskeyError(null);
|
|
setPasskeyBusy(true);
|
|
try {
|
|
const optRes = await fetch('/api/passkey/authenticate');
|
|
if (!optRes.ok) throw new Error('Optionen konnten nicht geladen werden.');
|
|
const optionsJSON = await optRes.json();
|
|
|
|
const response = await startAuthentication({ optionsJSON });
|
|
|
|
const verifyRes = await fetch('/api/passkey/authenticate', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ response }),
|
|
});
|
|
if (!verifyRes.ok) {
|
|
const data = await verifyRes.json().catch(() => null);
|
|
throw new Error(data?.error ?? 'Anmeldung mit Passkey fehlgeschlagen.');
|
|
}
|
|
window.location.href = '/';
|
|
} catch (err) {
|
|
// Abbruch durch Nutzer (NotAllowedError) nicht als Fehler anzeigen.
|
|
if (err instanceof Error && err.name === 'NotAllowedError') {
|
|
setPasskeyError(null);
|
|
} else {
|
|
setPasskeyError(err instanceof Error ? err.message : 'Anmeldung mit Passkey fehlgeschlagen.');
|
|
}
|
|
} finally {
|
|
setPasskeyBusy(false);
|
|
}
|
|
}
|
|
|
|
const version = packageJson.version;
|
|
const buildDate = process.env.NEXT_PUBLIC_BUILD_DATE || new Date().toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' });
|
|
|
|
return (
|
|
<div className="login-wrapper">
|
|
<main className="login-outer">
|
|
<div className="login-header">
|
|
<h1 className="login-page-title">Tabletten-Übersicht</h1>
|
|
</div>
|
|
|
|
<div className="login-center">
|
|
<div className="login-card">
|
|
<h2 className="login-card-title">Anmeldung</h2>
|
|
|
|
<form action={loginAction} className="login-form">
|
|
<div>
|
|
<label htmlFor="username" className="login-label">
|
|
Benutzername
|
|
</label>
|
|
<input
|
|
id="username"
|
|
name="username"
|
|
type="text"
|
|
required
|
|
autoComplete="off"
|
|
className="login-input"
|
|
placeholder="Benutzername"
|
|
disabled={isPending}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="password" className="login-label">
|
|
Passwort
|
|
</label>
|
|
<div className="login-password-wrapper">
|
|
<input
|
|
id="password"
|
|
name="password"
|
|
type={showPassword ? 'text' : 'password'}
|
|
required
|
|
autoComplete="new-password"
|
|
className="login-input login-input-password"
|
|
placeholder="Passwort"
|
|
disabled={isPending}
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPassword(v => !v)}
|
|
tabIndex={-1}
|
|
aria-label={showPassword ? 'Passwort verbergen' : 'Passwort anzeigen'}
|
|
className="login-eye-btn"
|
|
>
|
|
{showPassword ? (
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 4.411m0 0L21 21" />
|
|
</svg>
|
|
) : (
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
|
|
</svg>
|
|
)}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{state?.error && (
|
|
<div className="login-error">
|
|
{state.error}
|
|
</div>
|
|
)}
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={isPending}
|
|
className="login-submit"
|
|
>
|
|
{isPending ? 'Anmeldung läuft...' : 'Anmelden'}
|
|
</button>
|
|
</form>
|
|
|
|
<div className="login-divider">
|
|
<span>oder</span>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={handlePasskeyLogin}
|
|
disabled={passkeyBusy}
|
|
className="login-passkey-btn"
|
|
>
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z" />
|
|
</svg>
|
|
{passkeyBusy ? 'Anmeldung läuft...' : 'Mit Passkey anmelden'}
|
|
</button>
|
|
|
|
{passkeyError && (
|
|
<div className="login-error login-error-passkey">
|
|
{passkeyError}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<footer className="login-footer">
|
|
<div>
|
|
<a href="mailto:rxf@gmx.de">mailto:rxf@gmx.de</a>
|
|
</div>
|
|
<div>
|
|
Version {version} - {buildDate}
|
|
</div>
|
|
</footer>
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|