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 (
{title}

Verify Identity

Enter your 6-digit Exam PIN to unlock this paper.

{ const val = e.target.value.replace(/\D/g, ""); if (val.length <= 6) setPin(val); if (error) setError(""); }} className="w-48 text-center text-3xl tracking-[12px] font-bold py-3 bg-muted/30 border-2 border-border focus:border-primary focus:ring-4 focus:ring-primary/10 outline-none rounded-xl transition-all" placeholder="******" />
{error && ( {error} )}

Forgot your PIN? Reset it in Settings.

); }