Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recaptcha env usage #72

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/app/api/newsletter/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
import mailchimp from "@mailchimp/mailchimp_marketing";

// Validate required environment variables
const requiredEnvVars = ["MAILCHIMP_API_KEY", "MAILCHIMP_LIST_ID", "MAILCHIMP_SERVER_PREFIX"];
const requiredEnvVars = ["MAILCHIMP_API_KEY", "MAILCHIMP_LIST_ID", "MAILCHIMP_SERVER_PREFIX", "RECAPTCHA_SECRET_KEY"];
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
console.error(`Missing required environment variable: ${envVar}`);
Expand All @@ -29,6 +29,11 @@ function isValidEmail(email) {
}

async function verifyRecaptcha(token) {
if (!process.env.RECAPTCHA_SECRET_KEY) {
console.error("Missing RECAPTCHA_SECRET_KEY");
return false;
}

try {
const response = await fetch("https://www.google.com/recaptcha/api/siteverify", {
method: "POST",
Expand All @@ -39,6 +44,11 @@ async function verifyRecaptcha(token) {
});

const data = await response.json();

if (!data.success) {
console.error("reCAPTCHA verification failed:", data["error-codes"]);
}

return data.success;
} catch (error) {
console.error("reCAPTCHA verification failed:", error);
Expand Down
4 changes: 3 additions & 1 deletion src/components/Newsletter/Newsletter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Newsletter = () => {
const [isFocused, setIsFocused] = useState(false);
const reCaptchaRef = useRef(null);

const siteKey = process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY;
const siteKey = process.env.RECAPTCHA_SITE_KEY;

const handleChange = (e) => {
setEmail(e.target.value);
Expand All @@ -34,6 +34,8 @@ const Newsletter = () => {
const handleSubmit = async (e) => {
e.preventDefault();

console.log("Submitting with token:", token);

if (isSubmitting) return;
if (!email) {
setStatus("Error");
Expand Down