Spaces:
Sleeping

fx-craft / src /components /auth /TwoFactorVerify.tsx
felix-fx's picture
Initial setup for fx-craft website
9694b21 verified
Raw History Blame Contribute Delete
3.05 kB
/**
* 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;