File size: 5,633 Bytes
5463318
 
 
 
 
38ee1b6
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38ee1b6
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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>
    );
}