diff --git a/src/actions/init-session.ts b/src/actions/init-session.ts index 49a3b8dd..5526cca0 100644 --- a/src/actions/init-session.ts +++ b/src/actions/init-session.ts @@ -1,21 +1,40 @@ -import { app } from "@/firebase/server"; +import { app } from "@/lib/firebase/server"; import { defineAction } from "astro:actions"; import { getAuth } from "firebase-admin/auth"; import { z } from "astro:schema"; -import { initUserSubmission } from "@/firebase/database"; +import { initUserSubmission } from "@/lib/firebase/database"; +import { isCaptchaValid } from "@/lib/captcha"; export const initSession = defineAction({ accept: "json", input: z.object({ - idToken: z.string() + idToken: z.string(), + captchaToken: z.string().optional() }), - handler: async ({ idToken }, { cookies }) => { + handler: async ({ idToken, captchaToken }, { cookies }) => { const auth = getAuth(app); + /* Validate inputs */ + if (!captchaToken && import.meta.env.CAPTCHA_ENABLED === "true") { + return { + error: "Captcha token is required" + }; + } if (!idToken) { return { error: "No idToken provided" }; } + + /* Validate captcha */ + if (captchaToken && import.meta.env.CAPTCHA_ENABLED === "true") { + const isValid = await isCaptchaValid(captchaToken); + if (!isValid) { + return { + error: "Invalid captcha" + }; + } + } + /* Verify id token and save user to database */ try { const decodedToken = await auth.verifyIdToken(idToken); diff --git a/src/actions/submit-answers.ts b/src/actions/submit-answers.ts index b3786683..f98c0348 100644 --- a/src/actions/submit-answers.ts +++ b/src/actions/submit-answers.ts @@ -1,8 +1,8 @@ -import { app } from "@/firebase/server"; +import { app } from "@/lib/firebase/server"; import { defineAction } from "astro:actions"; import { getAuth } from "firebase-admin/auth"; import { z } from "astro:schema"; -import { saveAnswers } from "@/firebase/database"; +import { saveAnswers } from "@/lib/firebase/database"; export const submitAnswers = defineAction({ accept: "json", diff --git a/src/components/survey/section.tsx b/src/components/survey/section.tsx index 0ff05781..f0a5c0ff 100644 --- a/src/components/survey/section.tsx +++ b/src/components/survey/section.tsx @@ -19,7 +19,6 @@ export const ERRORS = { const convertAnswersToNumber = ( answers: Record ) => { - console.log(answers); const convertedAnswers: Record = {}; for (const [key, value] of Object.entries(answers)) { @@ -131,7 +130,7 @@ export default React.memo(({ section, next, setProgress }: SectionProps) => {