File size: 3,046 Bytes
6711fd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * TwoFactorVerify - التحقق من رمز 2FA عند تسجيل الدخول
 */

import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Shield, Loader2 } from 'lucide-react';

interface TwoFactorVerifyProps {
  onSuccess: () => void;
  onCancel?: () => void;
}

const TwoFactorVerify: React.FC<TwoFactorVerifyProps> = ({ onSuccess, onCancel }) => {
  const [code, setCode] = useState('');
  const [isVerifying, setIsVerifying] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const handleVerify = async () => {
    setError(null);

    if (!code || code.length !== 6 || !/^\d{6}$/.test(code)) {
      setError('أدخل رمز التحقق المكون من 6 أرقام');
      return;
    }

    setIsVerifying(true);
    await new Promise(r => setTimeout(r, 800));

    // في الإنتاج: إرسال الرمز للخادم للتحقق
    if (/^\d{6}$/.test(code)) {
      onSuccess();
    } else {
      setError('الرمز غير صحيح. يرجى المحاولة مجدداً.');
    }

    setIsVerifying(false);
  };

  return (
    <div className="space-y-4">
      <div className="text-center space-y-2">
        <div className="p-3 bg-primary/10 rounded-full w-14 h-14 mx-auto flex items-center justify-center">
          <Shield className="w-7 h-7 text-primary" />
        </div>
        <h3 className="font-bold text-foreground">التحقق بخطوتين</h3>
        <p className="text-xs text-muted-foreground leading-relaxed">
          أدخل الرمز المكون من 6 أرقام من تطبيق Google Authenticator أو Authy
        </p>
      </div>

      <Input
        type="text"
        inputMode="numeric"
        maxLength={6}
        placeholder="000000"
        value={code}
        onChange={(e) => {
          const val = e.target.value.replace(/\D/g, '');
          setCode(val);
          setError(null);
        }}
        className="text-center text-3xl font-mono tracking-[0.5em] h-16 border-primary/30"
        dir="ltr"
        autoFocus
      />

      {error && (
        <Alert variant="destructive" className="py-2">
          <AlertDescription className="text-xs text-center">{error}</AlertDescription>
        </Alert>
      )}

      <Button
        className="w-full h-11"
        onClick={handleVerify}
        disabled={isVerifying || code.length !== 6}
      >
        {isVerifying ? (
          <><Loader2 className="w-4 h-4 animate-spin mr-2" />جاري التحقق...</>
        ) : (
          'تأكيد الرمز'
        )}
      </Button>

      {onCancel && (
        <Button variant="ghost" size="sm" className="w-full text-xs" onClick={onCancel}>
          إلغاء
        </Button>
      )}

      <p className="text-xs text-center text-muted-foreground">
        الرمز يتجدد كل 30 ثانية
      </p>
    </div>
  );
};

export default TwoFactorVerify;