sepiariver / recaptchav2

Google's ReCaptcha V2 + V3 for MODX CMS: https://sepiariver.com/modx/recaptchav2-supports-recaptchav3/
https://sepiariver.com/
GNU General Public License v2.0
21 stars 15 forks source link

Cannot set token_key by scriptProperties #37

Closed sdrenth closed 5 years ago

sdrenth commented 5 years ago

I'm having two forms on the same page and I'm having issue because the token_key is the same for both forms. Currently it is not possible to set the token_key by scriptProperties,

My suggested fix would be to change:

$token_key = $modx->getOption('recaptchav3.token_key', null, 'recaptcha-token', true);

Into:

$token_key = $modx->getOption('recaptchav3.token_key', $scriptProperties, 'recaptcha-token', true);
sepiariver commented 5 years ago

Thanks @sdrenth

We can do this in the render snippet:

$token_key = $modx->getOption('recaptchav3.token_key', $scriptPropertis, $modx->getOption('recaptchav3.token_key', null, 'recaptcha-token', true), true);

This falls back to the system setting before using the hard-coded value.

However, both the render snippet and the form hook snippet need this value. If you set it for one you will need to set it for both.

I’ll look into getting this in for the PL milestone...

Quentin100 commented 5 years ago

Hi,

I have 3 forms on every page that use recaptchav3 as a hook in FormIt, but I can't get it to work.

I've set the recaptchav3_render with a recaptchav3.token_key for each one, and a unique action (the form ID) and it creates the token on each one and they are all unique, but it still fails. I also updated the code in both the hook and render snippets. Is this because I need to set the token_key value as a script property for the hook somehow? If so, how would I do that in FormIt?

Edit. Could I set them on the Properties Tab of recaptchav3 and if so, could I use a different Property value, eg recaptchav3.token_key = tk-[[+form_id]]

sepiariver commented 5 years ago

Hi @Quentin100

Is this because I need to set the token_key value as a script property for the hook somehow?

Yes you do it by adding the properties to the FormIt call:


[[!FormIt?
    &hooks=`recaptchav3`
    &recaptchav3.action_key=`my-action-key`
    &recaptchav3.token_key=`my-token-key`
]]
sepiariver commented 5 years ago

Released in ed3742c8f6f7f164b85be2edfdb75706cc5ad546

sepiariver commented 5 years ago

Multiple form example from README

<h2>Form Test 1</h2>
[[!FormIt?
    &hooks=`recaptchav3,FormItSaveForm`
    &validate=`testing1:required:minLength=^12^`
    &formName=`form-test-1`
    &recaptchav3.token_key=`token-1`
    &recaptchav3.action_key=`action-1`
    &submitVar=`submit1`
]]

<form action="[[~[[*id]]? &scheme=`full`]]" method="POST">
    [[!+fi.error.testing1]]
    <input type="text" name="testing1" value="[[!+fi.testing1]]">
    <input type="submit" name="submit1" value="submit">
    [[!recaptchav3_render?
        &tpl=`recaptchav3_html`
        &token_key=`token-1`
        &action_key=`action-1`
    ]]
    [[!+fi.error.recaptchav3_error]]
</form>

<h2>Form Test 2</h2>
[[!FormIt?
    &hooks=`recaptchav3,FormItSaveForm`
    &validate=`testing2:required:minLength=^12^`
    &formName=`form-test-2`
    &recaptchav3.token_key=`token-2`
    &recaptchav3.action_key=`action-2`
    &submitVar=`submit2`
]]

<form action="[[~[[*id]]? &scheme=`full`]]" method="POST">
    [[!+fi.error.testing2]]
    <input type="text" name="testing2" value="[[!+fi.testing2]]">
    <input type="submit" name="submit2" value="submit">
    [[!recaptchav3_render?
        &tpl=`recaptchav3_html`
        &token_key=`token-2`
        &action_key=`action-2`
    ]]
    [[!+fi.error.recaptchav3_error]]
</form>