plugin development – how to authenticate email and phone number separately in ajax login form?

Question

I’m writing my own login and register plugin and I’m using Ajax in this plugin…so I want to set an option for site admin if he/she wants to enable login with phone number(I’ve already write phone number field and validation codes for register form and it works correct and stores data in user meta 'mobile' key )

this is my login form :

<?php if ( ! isset( $wp_auth_options['is_phone_active'] ) || empty( $wp_auth_options['is_phone_active'] )
 
) { ?>
                <div class="form-row">
                    <label for="userEmail"><span class="name-input-forms">email</span>
                        <input type="email" name="userEmail" id="userEmail" class="errorField">
                    </label>
                </div>
<?php } else { ?>
                <div class="form-row">
                    <label for="phone"><span class="name-input-forms">phone number</span>
                        <input type="text" name="phone" id="phone" class="errorField">
                    </label>
                </div>
<?php } ?>

then I send data using Ajax in my auth.js file like this:

$('#loginForm').on('submit', function (event) {

        event.preventDefault();
        let user_email = $('#userEmail').val();
        let phone = $('#phone').val();
        let user_password = $('#userPassword').val();
        let notify = $('.alert');
        let border = $('.errorField');
        $.ajax({
            url: '/wp-admin/admin-ajax.php',
            type: 'post',
            dataType: 'json',
            data: {
                action: 'wp_auth_login',
                user_email: user_email,
                user_password: user_password
            },
            success: function (response) {
                if (response.success) {
                    setTimeout(function () {
                        window.location.href="https://wordpress.stackexchange.com/";
                    }, 2000);
                    notify.removeClass('alert-error').addClass('alert-success');
                    notify.html('<p>' + response.message + '</p>');
                    notify.css('display', 'block');
                    notify.delay(3000).hide(300);
                }
            },
            error: function (error) {
                if (error) {
                    let message = error.responseJSON.message;
                    notify.removeClass('alert-success').addClass('alert-error');
                    border.addClass('errorField');
                    notify.html('<p>' + message + '</p>');
                    notify.css('display', 'block');
                    border.css('border', '1px #e30b0b solid');
                    notify.delay(3000).hide(300);
                }
            }
        });


    });

now I have a unique validation for Email and password and I don’t know how to involve phone number validation in these codes:

here’s ajax.php:

function wp_auth_login_handle(){
    $user_email = sanitize_text_field($_POST['user_email']);
    $phone = sanitize_text_field($_POST['phone)']);
    $user_password = sanitize_text_field($_POST['user_password']);
    $validation_results = wp_auth_validation_email_and_password($user_email,$user_password);
    $validation_phone_login_results = wp_auth_validation_phone_and_password($phone,$user_password);
    if (!$validation_results['is_valid']){
        wp_send_json([
            'success' => false,
            'message' => $validation_results['message']
        ], 403);
    }
    if (!$validation_phone_login_results['is_valid']){
        wp_send_json([
            'success' => false,
            'message' => $validation_phone_login_results['message']
        ], 403);
    }
    if ($wp_auth_options['is_phone_active']){
        $has_user_mobile = get_user_meta($user->ID,'mobile');
        if (!$has_user_mobile){
            wp_send_json([
                'success' => false,
                'message' => 'custom message'
            ], 403);
        }
        $is_correct = preg_match("/^00989[0-9]{9}$/", $_POST['phone']);
        if ( $_POST['phone'] && !$is_correct) {
            wp_send_json([
                'success' => false,
                'message' => 'custom error message'
            ], 403);
        }
        function wp_auth_validation_phone_and_password($phone,$password){
            $result = [
                'is_valid'=>'true',
                'message'=>''
            ];
            if (is_null($phone) || empty($phone)){
                $result['is_valid'] = false;
                $result['message']='phone number can't be empty';
                return $result;
            }
            if (!is_numeric($phone)){
                $result['is_valid'] = false;
                $result['message']='phone number must be numeric';
                return $result;
            }
            if (is_null($password) || empty($password)){
                $result['is_valid'] = false;
                $result['message']= 'you must fill password field';
                return $result;
            }
            return $result;
        }
    }
    $user = wp_authenticate_email_password(null,$user_email,$user_password);
        if(is_wp_error($user)){
            wp_send_json([
                'success' => false,
                'message' => 'user not found!'
            ], 403);
        }

    $login_result = wp_signon([
        'user_login' => $user-> user_login,
        'user_password' => $user_password,
        'remember' => false
    ]);
        if (is_wp_error($login_result)){
            wp_send_json([
                'success' => false,
                'message' => 'there is something wrong! please try again later'
            ], 403);
        }
    wp_send_json([
        'success' => true,
        'message' => 'you have logged in successfully :)'
    ], 200);
}
function wp_auth_validation_email_and_password($email,$password){
    $result = [
        'is_valid'=>'true',
        'message'=>''
    ];
    if (is_null($email) || empty($email)){
        $result['is_valid'] = false;
        $result['message']='you must fill email field!';
        return $result;
    }
    if (is_null($password) || empty($password)){
        $result['is_valid'] = false;
        $result['message']= 'you must fill password field!';
        return $result;
    }
    if (!is_email($email)){
        $result['is_valid'] = false;
        $result['message'] = 'enter you email in right format(examle@test.com)';
        return $result;

    }
    return $result;
}

I tried so much and failed every time it works with email but when $wp_auth_options['is_phone_active'] has been selected it goes wrong

I don’t want get error message you must fill email field! when I’m using phone number authentication! so please help me in this pickle

0
navid 2 months 2021-05-25T02:40:41-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse