import React, { useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Lock, ShieldCheck, X, Loader2, AlertCircle } from 'lucide-react'; import { Button } from './Button'; import { supabase } from '../../utils/supabaseClient'; import { API_BASE_URL } from '../../utils/api'; export function ExamPinModal({ isOpen, onClose, onVerified, title = "Secure Access" }) { const [pin, setPin] = useState(""); const [isVerifying, setIsVerifying] = useState(false); const [error, setError] = useState(""); const handleVerify = async (e) => { if (e) e.preventDefault(); setError(""); if (!/^\d{6}$/.test(pin)) { setError("Please enter a 6-digit PIN"); return; } setIsVerifying(true); try { const { data: { session } } = await supabase.auth.getSession(); const response = await fetch(`${API_BASE_URL}/api/verify-exam-password`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${session?.access_token}` }, body: JSON.stringify({ password: pin }) }); const result = await response.json(); if (response.ok && result.success) { onVerified(pin); setPin(""); onClose(); } else { setError(result.error || "Incorrect PIN. Please try again."); } } catch (err) { setError("Connection failed. Try again later."); console.error(err); } finally { setIsVerifying(false); } }; if (!isOpen) return null; return (
Enter your 6-digit Exam PIN to unlock this paper.
Forgot your PIN? Reset it in Settings.