This is about Recaptcha V2

I have an issue with implementing ReCaptcha v2 in my Elm code as used in Rails. I want to send the token as part of the request to the API where validations will take place. The issue is I can’t seem to find a way of getting the token to my elm code.

below are the custom fields of the form

viewFields =
            [ Fields.Submission
                { errors = errorMessagesForKeyLabel state.error "display_text" ""
                , label = animatedPrompt typeSpecificContent state
                , labelPlacement = typeSpecificContent.inputLabelPlacement
                , charactersRemainingText = getCharactersRemainingText typeSpecificContent state
                , value = state.questionText
                , message = UpdateQuestion
                }
            , Fields.SectionHeaderGroup
                { header = typeSpecificContent.contactInfoSectionHeader
                , subheader = typeSpecificContent.contactInfoSectionSubheader
                }
            , Fields.Text
                { errors = errorMessagesForKeyLabel state.error "name" typeSpecificContent.formNameLabel
                , label = typeSpecificContent.formNameLabel
                , labelPlacement = typeSpecificContent.inputLabelPlacement
                , value = state.askerName
                , message = UpdateName
                }
            , emailField
            , Fields.Recaptcha { id = "recaptcha-container" }
            ]

For the recaptcha we just define that empty div with that id and use google recaptcha api onload to display the recaptcha.

When recaptcha is verified how can i get the token to the Elm code

You can wrap the recaptcha in a “custom element” a.k.a. “web component”:

You can pass the token back to Elm via a custom event: Creating and triggering events - Event reference | MDN

Another option would be to use a port to send in the token: Ports · An Introduction to Elm

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.