Skip to content

Conversation

@eric-michel
Copy link
Contributor

What?

Adds a filter to allow a theme or plugin to turn off auto submission of authcodes

Why?

Plugins that enhance two-factor may want to turn off auto-submission to allow the user time to interact with other form elements on the page prior to submission. See #723.

How?

A filter is added and assigned to a variable. That variable is checked prior to the relevant JS being output (and prevents it from printing entirely if the value is false).

Testing Instructions

  • Install the branch and activate plugin
  • Verify normal auto-submission experience
  • In theme's functions.php, add the following:
add_filter( 'two_factor_auto_submit_authcode', '__return_false' );
  • Observe that the authcode form no longer auto-submits.

Changelog Entry

Added - Themes and plugins can now disable auto-submission of authcodes via the two_factor_auto_submit_authcode filter

Copy link
Collaborator

@kasparsd kasparsd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Had one optional suggestion for moving the PHP state into JS variable to help move these scripts into standalone JS eventually.

Could you please document the filter, add the docblock comment (similar to other filters)?

if ( undefined !== form.requestSubmit ) {
form.requestSubmit();
form.submit.disabled = "disabled";
<?php if ( $auto_submit_authcode ) { ?>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the variable state to generate a JS variable instead? That would allow us to eventually move this JS into an external file and pass that as data to the script.

Something like const autoSubmitEnabled = <?php echo json_encode( ... ); ?> and then use that here in pure JS if-check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to make this change! How/where would you like the JS variable to be output? I could output it as a data attribute on the form element (something like data-auto-submit) or could output it as a small <script> snippet in the <head> tag so it's a global variable that can be accessed. I kind of lean toward the data attribute to keep it scoped to the form itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasparsd any thoughts on the question above on approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasparsd @jeffpaul I opted to add a data attribute on the form and check for that in the JS if statement. Seems pretty clean to me. Take a look and let me know what you think!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add hook to turn off auto-submission of 2FA form

3 participants