Next.js Discord

Discord Forum

How to integrate ReCaptcha with Server Actions?

Unanswered
valse posted this in #help-forum
Open in Discord
Hi, I'm trying to swap from an API based form request to the server actions solution: it works like a charm but now I'd like to integrate ReCaptcha too; I'm using the GoogleReCaptcha component to generate the token... it works but it's not the best solution because the token is generated too early. Any suggestions? Thanks!

2 Replies

I solved passing to the action method a third parameter that is the token from the reCaptcha execution:
const [state, formAction] = useFormState(
    async (
      prevState: {
        message: string
        success: boolean
      },
      formData: FormData
    ) => send(prevState, formData, await getToken()),
    initialState
)