Authenticate WooCommerce custom login fields.

Which plugin are you using to create the custom login fields? If not a plugin, can you share the code you’ve written?

You will likely get more replies if you provide more detail.

@littlepackage Thanks for your response I have found a solution..

Below is the solution I found. It might be helpful for someone else

//login authentication
function login_auth($user)
{
$login_otp = wc()->session->get(‘login_otp’);

if( empty($_POST[‘otp_login_field’])){

remove_action(‘authenticate’, ‘wp_authenticate_username_password’, 20);
$user = new WP_Error( ‘denied’, __(” Otp field is empty “));
}
elseif($_POST[‘otp_login_field’] != $login_otp){
remove_action(‘authenticate’, ‘wp_authenticate_username_password’, 20);
$user = new WP_Error( ‘denied’, __(” Invalid otp “));

}

return $user;
}//login authentication ends here

add_filter(‘woocommerce_process_login_errors’, array($this,’login_auth’),10,3);


// login authentication
function my_custom_otp_login_auth( $validation_error, $login $password ) {

    $login_otp = WC()->session->get( 'login_otp' );

    if ( empty( $_POST['otp_login_field'] ) ) {
        remove_action( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
        $validation_error = new WP_Error( 'denied', 'OTP field is empty' );
    } else if ( isset( $login_otp ) && $_POST['otp_login_field'] != $login_otp ) {
        remove_action( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
        $validation_error = new WP_Error( 'denied', 'Invalid OTP');
    }
    return $validation_error;
}
add_filter( 'woocommerce_process_login_errors', 'my_custom_otp_login_auth', 10, 3 );

I tidied up your code so someone might be able to use it as-is, but they should know this could use more work to be safe & ready-to-use. It’d be even more helpful to the community if you shared which plugin you are using to create the custom login fields… I looked a bit and still can’t guess which plugin this works with. If not a plugin, then maybe share the code for the ‘otp_login_field’ field(s).



Source link