fix: unsubscribe from the newsletter right after solving the CAPTCHA on the unsubscription form
Some checks failed
Deploy Next.js application / deploy (push) Has been cancelled

This commit is contained in:
Dorian Niemiec 2024-09-09 15:56:37 +02:00
parent 004ecc5c60
commit bdc679376d

View file

@ -13,14 +13,13 @@ const UnsubscribePage = ({
const { toast } = useToast(); const { toast } = useToast();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [showCaptcha, setShowCaptcha] = useState(false); const [showCaptcha, setShowCaptcha] = useState(false);
const [captchaToken, setCaptchaToken] = useState<string | null>(null);
const onSubmit = async () => { const handleCaptchaVerify = async (token: string) => {
if (!captchaToken) { setShowCaptcha(false);
setShowCaptcha(true); await submit(token); // Trigger form submission after captcha is verified
return; };
}
const submit = async (captchaToken: string) => {
setLoading(true); setLoading(true);
try { try {
const res = await fetch("/api/unsubscribe", { const res = await fetch("/api/unsubscribe", {
@ -33,7 +32,6 @@ const UnsubscribePage = ({
}); });
if (res.ok) { if (res.ok) {
setCaptchaToken(null); // Reset captcha token after successful submission
toast({ toast({
description: "Unsubscribed successfully." description: "Unsubscribed successfully."
}); });
@ -55,11 +53,6 @@ const UnsubscribePage = ({
} }
}; };
const handleCaptchaVerify = async (token: string) => {
setCaptchaToken(token);
await onSubmit(); // Trigger form submission after captcha is verified
};
return ( return (
<section <section
id="vulnerabilities" id="vulnerabilities"
@ -76,7 +69,7 @@ const UnsubscribePage = ({
className="mx-auto text-center" className="mx-auto text-center"
onSubmit={(e: React.FormEvent) => { onSubmit={(e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
onSubmit(); setShowCaptcha(true);
}} }}
> >
<Button <Button