4a3dfa5821
- Tabelle passkeys + lib/passkeys.ts (CRUD, Counter-Replay-Schutz)
- lib/webauthn.ts: RP-Config via RP_ID/RP_ORIGIN/RP_NAME, Challenge im
httpOnly-Cookie, Wrapper um @simplewebauthn/server
- API: app/api/passkey/{register,authenticate,route}; authenticate ist
öffentlich (proxy.ts ausgenommen) und setzt bei Erfolg die Session
- Login-Button "Mit Passkey anmelden", Verwaltung im Einstellungen-Tab
- Passwort bleibt als Fallback; Version 0.1.1
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
171 lines
7.7 KiB
TypeScript
171 lines
7.7 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="min-h-screen bg-white py-4 px-4">
|
|
<main className="max-w-6xl mx-auto border-2 border-black rounded-lg p-6 bg-[#EEF7EE]">
|
|
<div className="flex justify-between items-center mb-6">
|
|
<h1 className="text-3xl font-bold">Arbeitszeiterfassung</h1>
|
|
</div>
|
|
|
|
<div className="flex justify-center py-10">
|
|
<div className="w-full max-w-sm bg-white border border-gray-300 rounded-xl shadow-md p-8">
|
|
<h2 className="text-xl font-semibold text-gray-900 mb-6 text-center">Anmeldung</h2>
|
|
|
|
<form action={loginAction} className="space-y-5">
|
|
<div>
|
|
<label htmlFor="username" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Benutzer
|
|
</label>
|
|
<input
|
|
id="username"
|
|
name="username"
|
|
type="text"
|
|
required
|
|
defaultValue="Birgit"
|
|
autoComplete="username"
|
|
className="w-full px-3 py-2 border-2 border-gray-400 rounded-lg bg-white text-gray-900 focus:border-green-600 focus:outline-none text-sm"
|
|
placeholder="Benutzer"
|
|
disabled={isPending}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Passwort
|
|
</label>
|
|
<div className="relative">
|
|
<input
|
|
id="password"
|
|
name="password"
|
|
type={showPassword ? 'text' : 'password'}
|
|
required
|
|
autoComplete="current-password"
|
|
className="w-full px-3 py-2 pr-10 border-2 border-gray-400 rounded-lg bg-white text-gray-900 focus:border-green-600 focus:outline-none text-sm"
|
|
placeholder="Passwort"
|
|
disabled={isPending}
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPassword((v) => !v)}
|
|
tabIndex={-1}
|
|
aria-label={showPassword ? 'Passwort verbergen' : 'Passwort anzeigen'}
|
|
className="absolute inset-y-0 right-0 px-3 flex items-center text-gray-500 hover:text-gray-800"
|
|
>
|
|
{showPassword ? (
|
|
<svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" 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" className="h-5 w-5" 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="bg-red-50 border border-red-300 text-red-700 px-3 py-2 rounded-lg text-sm">
|
|
{state.error}
|
|
</div>
|
|
)}
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={isPending}
|
|
className="w-full py-2 px-4 bg-[#86C28B] hover:bg-[#6FB075] text-black font-medium rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed text-sm"
|
|
>
|
|
{isPending ? 'Anmeldung läuft...' : 'Anmelden'}
|
|
</button>
|
|
</form>
|
|
|
|
<div className="flex items-center gap-3 my-5">
|
|
<div className="h-px flex-1 bg-gray-200" />
|
|
<span className="text-xs text-gray-400">oder</span>
|
|
<div className="h-px flex-1 bg-gray-200" />
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={handlePasskeyLogin}
|
|
disabled={passkeyBusy}
|
|
className="w-full py-2 px-4 flex items-center justify-center gap-2 border-2 border-gray-400 hover:border-green-600 text-gray-900 font-medium rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed text-sm"
|
|
>
|
|
<svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" 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="mt-4 bg-red-50 border border-red-300 text-red-700 px-3 py-2 rounded-lg text-sm">
|
|
{passkeyError}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<footer className="mt-8 flex justify-between items-center text-sm text-gray-600 px-4">
|
|
<div>
|
|
<a href="mailto:rxf@gmx.de" className="text-green-700 hover:underline">
|
|
rxf@gmx.de
|
|
</a>
|
|
</div>
|
|
<div className="text-right">
|
|
Version {version} — {buildDate}
|
|
</div>
|
|
</footer>
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|