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-02-14 11:47:26 +01:00
|
|
|
import styles from './PasswordGate.module.css';
|
2026-02-14 10:30:27 +01:00
|
|
|
|
|
|
|
|
interface PasswordGateProps {
|
2026-02-14 11:47:26 +01:00
|
|
|
slug: string;
|
|
|
|
|
title: string;
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function PasswordGate({ slug, title }: PasswordGateProps) {
|
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' },
|
|
|
|
|
body: JSON.stringify({ slug, password }),
|
|
|
|
|
});
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
if (res.ok) {
|
|
|
|
|
window.location.reload();
|
|
|
|
|
} else {
|
2026-03-27 11:11:23 +01:00
|
|
|
setError('Incorrect password. Please try again.');
|
2026-02-14 11:47:26 +01:00
|
|
|
setPassword('');
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
2026-03-27 11:11:23 +01:00
|
|
|
setError('Unable to verify password. Please try again later.');
|
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>
|
|
|
|
|
<p className={styles.subtitle}>This gallery is password-protected.</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)}
|
|
|
|
|
placeholder="Enter password"
|
2026-03-22 09:57:29 +01:00
|
|
|
aria-label="Enter password"
|
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}>
|
|
|
|
|
{loading ? 'Verifying…' : 'Enter'}
|
|
|
|
|
</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>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|