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 {
|
|
|
|
|
setError('Wrong password');
|
|
|
|
|
setPassword('');
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
setError('Something went wrong');
|
|
|
|
|
} 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
|
|
|
|
|
type="password"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
placeholder="Enter password"
|
|
|
|
|
className={styles.input}
|
|
|
|
|
autoFocus
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<button type="submit" className={styles.button} disabled={loading}>
|
|
|
|
|
{loading ? 'Verifying…' : 'Enter'}
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
{error && <p className={styles.error}>{error}</p>}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|