()
| 7 | import { useToast } from '../components/Toast/ToastProvider'; |
| 8 | |
| 9 | export default function VerifyEmail() { |
| 10 | const { t, i18n } = useTranslation(); |
| 11 | const toast = useToast(); |
| 12 | const [searchParams] = useSearchParams(); |
| 13 | const navigate = useNavigate(); |
| 14 | const location = useLocation(); |
| 15 | const { setAuth, user } = useAuthStore(); |
| 16 | |
| 17 | // Get email from location state (passed from register) or from URL params |
| 18 | const [email, setEmail] = useState<string>((location.state as any)?.email || searchParams.get('email') || user?.email || ''); |
| 19 | |
| 20 | // Support both 'token' and 'code' from URL |
| 21 | const urlToken = searchParams.get('token') || searchParams.get('code'); |
| 22 | |
| 23 | const [code, setCode] = useState(urlToken || ''); |
| 24 | const [status, setStatus] = useState<'idle' | 'verifying' | 'success' | 'error'>('idle'); |
| 25 | const [message, setMessage] = useState(''); |
| 26 | const [loading, setLoading] = useState(false); |
| 27 | |
| 28 | const isChinese = i18n.language?.startsWith('zh'); |
| 29 | |
| 30 | useEffect(() => { |
| 31 | if (user?.is_active) { |
| 32 | navigate('/'); |
| 33 | } |
| 34 | }, [user, navigate]); |
| 35 | |
| 36 | const handleVerify = async (tokenToUse: string) => { |
| 37 | if (!tokenToUse || tokenToUse.length < 6) return; |
| 38 | |
| 39 | setLoading(true); |
| 40 | setStatus('verifying'); |
| 41 | setError(''); |
| 42 | |
| 43 | try { |
| 44 | const res = await authApi.verifyEmail(tokenToUse); |
| 45 | setStatus('success'); |
| 46 | setMessage(isChinese ? '邮箱验证成功!' : 'Email verified successfully!'); |
| 47 | |
| 48 | // Auto-login with the returned token |
| 49 | if (res.access_token && res.user) { |
| 50 | setAuth(res.user, res.access_token); |
| 51 | |
| 52 | // Redirect based on needs_company_setup |
| 53 | setTimeout(() => { |
| 54 | if (res.needs_company_setup) { |
| 55 | navigate('/setup-company'); |
| 56 | } else { |
| 57 | navigate('/'); |
| 58 | } |
| 59 | }, 1500); // Short delay to show success message |
| 60 | } |
| 61 | } catch (err: any) { |
| 62 | setStatus('error'); |
| 63 | setMessage(err.message || (isChinese ? '验证失败,请检查验证码是否正确' : 'Verification failed, please check the code')); |
| 64 | } finally { |
| 65 | setLoading(false); |
| 66 | } |
nothing calls this directly
no test coverage detected