110 lines
3.3 KiB
TypeScript
110 lines
3.3 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
interface AuthGateProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
const SESSION_KEY = 'auth_session';
|
|
|
|
export default function AuthGate({ children }: AuthGateProps) {
|
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
const [isChecking, setIsChecking] = useState(true);
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState('');
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
// Check for existing session on mount
|
|
useEffect(() => {
|
|
const session = sessionStorage.getItem(SESSION_KEY);
|
|
if (session === 'authenticated') {
|
|
setIsAuthenticated(true);
|
|
}
|
|
setIsChecking(false);
|
|
}, []);
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setError('');
|
|
setIsSubmitting(true);
|
|
|
|
try {
|
|
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || '';
|
|
const response = await fetch(`${basePath}/api/auth`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok && data.authenticated) {
|
|
sessionStorage.setItem(SESSION_KEY, 'authenticated');
|
|
setIsAuthenticated(true);
|
|
setPassword('');
|
|
} else {
|
|
setError('Invalid password');
|
|
}
|
|
} catch (err) {
|
|
setError('Authentication failed');
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
}
|
|
|
|
if (isChecking) {
|
|
return (
|
|
<div className="flex items-center justify-center min-h-screen">
|
|
<div className="text-gray-600">Loading...</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
return (
|
|
<div className="flex items-center justify-center min-h-screen bg-gray-50 p-4">
|
|
<div className="w-full max-w-md">
|
|
<div className="bg-white rounded-lg shadow-lg p-8">
|
|
<h1 className="text-2xl font-bold text-center mb-6">
|
|
Authentication Required
|
|
</h1>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div>
|
|
<label
|
|
htmlFor="password"
|
|
className="block text-sm font-medium text-gray-700 mb-2"
|
|
>
|
|
Password
|
|
</label>
|
|
<input
|
|
id="password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
|
|
placeholder="Enter password"
|
|
disabled={isSubmitting}
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
{error && (
|
|
<div className="text-red-600 text-sm">{error}</div>
|
|
)}
|
|
<button
|
|
type="submit"
|
|
disabled={isSubmitting || !password}
|
|
className="w-full bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
|
>
|
|
{isSubmitting ? 'Authenticating...' : 'Sign In'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|