How can i get rid of the multiple SMS auth?

75 views Asked by At

I posted my code examples below. Basically the whole idea is to make a simple authentification with a phone number and generated 6-digit code. User is posting a code in a field and it is done. But i have a slight problem. Every time i refresh a page site is asking to make an auth again. Or if i use a function to get rid of it, it starts to make a multiple SMS with codes. Frontend is JS and backend is PHP.

Here is my frontend auth code

$(document).on('submit', "#reg", function(e) {
    e.preventDefault();
    var form = $(this);

    var enteredPhone = $('#phone').val();
    sendFormToServer(form, function(data) {
        $('#error-message').text("");
        $('#reg').hide();
        $('#regButton').hide();
        $('#verificationForm').show();
        
        verificationCode = data.verification_code;
        
    });
});
function sendFormToServer(form, callback) {
    $.ajax({
        type: "POST",
        url: actionUrl,
        data: form.serialize(),
        dataType: 'json',
        success: function(data) {
            callback(data);
        },
        error: function(xhr, status, error) {
            handleServerError(xhr);
        }
    });
}
function lk_auth(data) {

    const obj = data;
    if (obj.sendt_to) {
        setCookie('auth', true);
        setCookie('contactID', obj.sendt_to, options = {});
        setCookie('phone', obj.phone, options = {});
        console.log('auth = ' + getCookie('auth') + ' contactID = ' + getCookie('contactID') + 'phone = ' + getCookie('phone'));

        const $menu = $('<div class="header__menu"></div>');


    $menu.append('<span style="display:none" id="documents"></span >');
        $menu.append('<span style="display:none" id="chat"></span >');
        $menu.append('<span style="display:none" id="payments"></span >');
  
        $('#reg').hide();
    $('#verificationForm').hide();
        $('#body').append($menu);
    $('#error-message').hide();
    }
    

    const $content = $('<div class="div0"></div>');
    $content.append('<div class="header__menu">Phone: ' + getCookie('phone') + '</div');
    $content.find('.header__menu').css('display', 'none');

    $('#content').empty();
    $('#content').append($content);
}

Now the backend of making the SMS itself

session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['phone'])) {
        $phone = trim(str_replace(array('-', ' '), '', $_POST['phone']));
        $contactData['msg'] = 'Телефон: ' . $phone;
        $resultContactID = CRest::call(
            'crm.duplicate.findbycomm',
            array(
                'entity_type' => "CONTACT",
                'type' => "PHONE",
                'values' => array($phone)
            )
        );

        if (empty($resultContactID['result']['CONTACT'])) {
            $contactData['msg'] .= ' No personal account is registered';
            http_response_code(403);
        } elseif (count($resultContactID['result']['CONTACT']) > 1) {
            $contactData['msg'] .= ' attention there is more than 1 contacts ' . count($resultContactID['result']['CONTACT']) . ' contacts!';
            http_response_code(500);
        } else {
            $contactData['ID'] = $resultContactID['result']['CONTACT'][0];
            $message = generateRandomCode();
            $api_token = '*****';
            $url = "https://sms.ru/sms/send?api_id=$api_token&to=$phone&msg=$message&json=1";
            $resultContactID = CRest::call(
                'crm.duplicate.findbycomm',
                array(
                    'entity_type' => "CONTACT",
                    'type' => "PHONE",
                    'values' => array($phone)
                )
            );

            $contactData['ID'] = $resultContactID['result']['CONTACT'][0];
            $response = file_get_contents($url);
            $result = json_decode($response);

            if ($result->status == "OK") {
                $_SESSION['verification_code'] = $message;
                $contactData['status'] = 'sent';
                $contactData['message'] = 'SMS sent successfully ';
                $contactData['sendt_to'] = $contactData['ID'];
                $contactData['verification_code'] = $message;
                $contactData['phone'] = $phone;
            } else {
                $contactData['status'] = 'error';
                $contactData['message'] = 'Error in sending SMS';
            }
        }

        echo json_encode($contactData, JSON_UNESCAPED_UNICODE);
        exit;
    }
}
0

There are 0 answers