import React, { useState } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; const OTPVerification: React.FC = () => { const [otp, setOtp] = useState(['', '', '', '', '', '']); const [isLoading, setIsLoading] = useState(false); const { verifyOTP } = useAuth(); const navigate = useNavigate(); const location = useLocation(); const email = location.state?.email || ''; const handleOtpChange = (index: number, value: string) => { if (value.length <= 1) { const newOtp = [...otp]; newOtp[index] = value; setOtp(newOtp); // Auto-focus next input if (value && index < 5) { const nextInput = document.getElementById(`otp-${index + 1}`); nextInput?.focus(); } } }; const handleKeyDown = (index: number, e: React.KeyboardEvent) => { if (e.key === 'Backspace' && !otp[index] && index > 0) { const prevInput = document.getElementById(`otp-${index - 1}`); prevInput?.focus(); } }; const handleVerify = async () => { const otpCode = otp.join(''); if (otpCode.length !== 6) return; setIsLoading(true); try { await verifyOTP(email, otpCode); navigate('/company-wiki'); } catch (error) { console.error('OTP verification failed:', error); // Reset OTP on error setOtp(['', '', '', '', '', '']); document.getElementById('otp-0')?.focus(); } finally { setIsLoading(false); } }; return (
Verify your email
Enter the 6-digit code sent to {email}
Verification Code
*
{otp.map((digit, index) => ( handleOtpChange(index, e.target.value)} onKeyDown={(e) => handleKeyDown(index, e)} className="w-12 h-12 bg-Neutrals-NeutralSlate100 rounded-xl text-center text-Neutrals-NeutralSlate950 text-lg font-semibold font-['Inter'] outline-none focus:bg-Neutrals-NeutralSlate200 focus:outline-2 focus:outline-Brand-Orange" maxLength={1} /> ))}
Didn't receive the code?
); }; export default OTPVerification;