ExamCraftAI / Frontend /src /components /ui /ExamPinModal.jsx
Hkoradiya123
Serve frontend and backend from one Space
38ee1b6
Raw History Blame Contribute Delete
5.63 kB
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 (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-background/80 backdrop-blur-sm">
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="w-full max-w-sm bg-card border border-border rounded-2xl shadow-2xl overflow-hidden"
>
<div className="p-1 px-4 border-b border-border flex justify-between items-center bg-muted/30">
<span className="text-xs font-bold uppercase tracking-widest text-muted-foreground flex items-center gap-2 py-2">
<Lock size={12} className="text-primary" /> {title}
</span>
<button onClick={onClose} className="p-1 hover:bg-muted rounded-full transition-colors">
<X size={16} />
</button>
</div>
<div className="p-6 text-center space-y-6">
<div className="mx-auto w-16 h-16 bg-primary/10 rounded-full flex items-center justify-center text-primary mb-2">
<ShieldCheck size={32} />
</div>
<div>
<h3 className="text-xl font-bold">Verify Identity</h3>
<p className="text-sm text-muted-foreground mt-1">
Enter your 6-digit Exam PIN to unlock this paper.
</p>
</div>
<form onSubmit={handleVerify} className="space-y-4">
<div className="flex justify-center gap-2">
<input
autoFocus
type="password"
maxLength={6}
value={pin}
onChange={(e) => {
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="******"
/>
</div>
{error && (
<motion.div
initial={{ opacity: 0, y: -5 }}
animate={{ opacity: 1, y: 0 }}
className="flex items-center justify-center gap-2 text-destructive text-sm font-medium"
>
<AlertCircle size={14} /> {error}
</motion.div>
)}
<Button
type="submit"
className="w-full h-12 text-base shadow-lg shadow-primary/20"
disabled={isVerifying || pin.length < 6}
>
{isVerifying ? <Loader2 size={18} className="animate-spin mr-2" /> : "Verify Access"}
</Button>
</form>
<p className="text-xs text-muted-foreground">
Forgot your PIN? Reset it in <a href="/app/settings" className="text-primary font-bold hover:underline">Settings</a>.
</p>
</div>
</motion.div>
</div>
);
}