2026-02-14 10:30:27 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Password gate — shown when accessing a protected album/subpage.
|
|
|
|
|
* Submits to /api/auth and refreshes on success.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { useState } from 'react';
|
2026-10-02 18:14:23 +02:00
|
|
|
import Link from 'next/link';
|
2026-02-14 11:47:26 +01:00
|
|
|
import styles from './PasswordGate.module.css';
|
2026-08-23 23:18:14 +02:00
|
|
|
import { useDictionary } from './I18nProvider';
|
2026-02-14 10:30:27 +01:00
|
|
|
|
|
|
|
|
interface PasswordGateProps {
|
2026-02-14 11:47:26 +01:00
|
|
|
slug: string;
|
|
|
|
|
title: string;
|
2026-09-29 22:50:30 +02:00
|
|
|
type?: 'subpage' | 'album' | 'journal' | 'page' | 'site';
|
2026-10-02 18:14:23 +02:00
|
|
|
/**
|
|
|
|
|
* Links shown under the card. The site gate passes its legal pages here,
|
|
|
|
|
* which a locked site still serves (proxy.ts, UNGATED_PAGES).
|
|
|
|
|
*/
|
|
|
|
|
legalLinks?: { href: string; label: string }[];
|
2026-09-29 22:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Whole seconds from a `Retry-After` header, or null when there is none. */
|
|
|
|
|
function retryAfterSeconds(res: Response): number | null {
|
|
|
|
|
const seconds = Number.parseInt(res.headers.get('retry-after') ?? '', 10);
|
|
|
|
|
return Number.isFinite(seconds) && seconds > 0 ? seconds : null;
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-02 18:14:23 +02:00
|
|
|
export default function PasswordGate({
|
|
|
|
|
slug,
|
|
|
|
|
title,
|
|
|
|
|
type = 'subpage',
|
|
|
|
|
legalLinks = [],
|
|
|
|
|
}: PasswordGateProps) {
|
2026-08-23 23:18:14 +02:00
|
|
|
const t = useDictionary();
|
2026-02-14 11:47:26 +01:00
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setError('');
|
|
|
|
|
setLoading(true);
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
try {
|
|
|
|
|
const res = await fetch('/api/auth', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2026-04-07 23:04:03 +02:00
|
|
|
body: JSON.stringify({ slug, password, type }),
|
2026-02-14 11:47:26 +01:00
|
|
|
});
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
if (res.ok) {
|
|
|
|
|
window.location.reload();
|
2026-09-29 22:50:30 +02:00
|
|
|
} else if (res.status === 401) {
|
2026-08-23 23:18:14 +02:00
|
|
|
setError(t.password.incorrect);
|
2026-02-14 11:47:26 +01:00
|
|
|
setPassword('');
|
2026-09-29 22:50:30 +02:00
|
|
|
} else if (res.status === 429) {
|
|
|
|
|
// The limit says nothing about the password, which may well be right:
|
|
|
|
|
// keep it in the field and say when the next attempt will be heard.
|
|
|
|
|
setError(t.password.tooManyAttempts(retryAfterSeconds(res)));
|
|
|
|
|
} else {
|
|
|
|
|
setError(t.password.failed);
|
2026-02-14 11:47:26 +01:00
|
|
|
}
|
|
|
|
|
} catch {
|
2026-08-23 23:18:14 +02:00
|
|
|
setError(t.password.failed);
|
2026-02-14 11:47:26 +01:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
2026-02-14 11:47:26 +01:00
|
|
|
}
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
return (
|
|
|
|
|
<div className={styles.gate}>
|
|
|
|
|
<div className={styles.card}>
|
|
|
|
|
<h2 className={styles.title}>{title}</h2>
|
2026-08-23 23:18:14 +02:00
|
|
|
<p className={styles.subtitle}>
|
2026-09-29 22:50:30 +02:00
|
|
|
{type === 'site'
|
|
|
|
|
? t.password.siteSubtitle
|
|
|
|
|
: type === 'page'
|
|
|
|
|
? t.password.pageSubtitle
|
|
|
|
|
: type === 'journal'
|
|
|
|
|
? t.password.journalSubtitle
|
|
|
|
|
: t.password.subtitle}
|
2026-08-23 23:18:14 +02:00
|
|
|
</p>
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
<form onSubmit={handleSubmit} className={styles.form}>
|
|
|
|
|
<input
|
2026-03-27 10:36:22 +01:00
|
|
|
id="password-input"
|
2026-02-14 11:47:26 +01:00
|
|
|
type="password"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
2026-08-23 23:18:14 +02:00
|
|
|
placeholder={t.password.placeholder}
|
|
|
|
|
aria-label={t.password.placeholder}
|
2026-02-14 11:47:26 +01:00
|
|
|
className={styles.input}
|
|
|
|
|
autoFocus
|
|
|
|
|
required
|
2026-03-27 10:36:22 +01:00
|
|
|
disabled={loading}
|
|
|
|
|
aria-invalid={!!error}
|
|
|
|
|
aria-describedby={error ? 'password-error' : undefined}
|
2026-02-14 11:47:26 +01:00
|
|
|
/>
|
|
|
|
|
<button type="submit" className={styles.button} disabled={loading}>
|
2026-05-31 10:04:15 +02:00
|
|
|
{loading ? (
|
|
|
|
|
<>
|
|
|
|
|
<svg
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
width="16"
|
|
|
|
|
height="16"
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
fill="none"
|
|
|
|
|
stroke="currentColor"
|
|
|
|
|
strokeWidth="2"
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
className={styles.spinner}
|
|
|
|
|
>
|
|
|
|
|
<circle cx="12" cy="12" r="10" strokeOpacity="0.25"></circle>
|
|
|
|
|
<path d="M12 2a10 10 0 0 1 10 10"></path>
|
|
|
|
|
</svg>
|
2026-08-23 23:18:14 +02:00
|
|
|
{t.password.verifying}
|
2026-05-31 10:04:15 +02:00
|
|
|
</>
|
|
|
|
|
) : (
|
2026-08-23 23:18:14 +02:00
|
|
|
t.password.submit
|
2026-05-31 10:04:15 +02:00
|
|
|
)}
|
2026-02-14 11:47:26 +01:00
|
|
|
</button>
|
|
|
|
|
</form>
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-03-27 10:36:22 +01:00
|
|
|
{error && (
|
|
|
|
|
<p id="password-error" className={styles.error} role="alert" aria-live="polite">
|
|
|
|
|
{error}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2026-02-14 11:47:26 +01:00
|
|
|
</div>
|
2026-10-02 18:14:23 +02:00
|
|
|
{legalLinks.length > 0 && (
|
|
|
|
|
<nav className={styles.legal} aria-label={t.password.legalNavAria}>
|
|
|
|
|
{legalLinks.map(({ href, label }) => (
|
|
|
|
|
<Link key={href} href={href} className="footer__legal-link">
|
|
|
|
|
{label}
|
|
|
|
|
</Link>
|
|
|
|
|
))}
|
|
|
|
|
</nav>
|
|
|
|
|
)}
|
2026-02-14 11:47:26 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|