Welcome Guest, Not a member yet? Register   Sign In
CSRF regenerate with AJAX
#3

(This post was last modified: 04-15-2020, 04:33 AM by Leo.)

I think I found a solution. Of course no page reloading. CSRF set to regenerate. A lot of forms on a single page. Please test, or point out if this may not work in some scenarios, if you have the time. Heres a simple, handy, dandy, vanilla javascript function that updates ALL present forms on the page with current csrf token:
Code:
function update_csrf_fields(value) {
    let all_forms = document.forms;
    for(e of all_forms) {
        e.querySelector('input[name=csrf_token]').value = value;
    }
}
Please note if your csrf_token is named something else - change the input name. Heres full example cycle:

Form submit using JQuery:
Code:
$(".form").submit(function(e) {
    e.preventDefault();
    let form = this;
    let choice = $(e.target).find("input[type=submit]:focus");
    if (choice) {
        if (choice[0].className === 'delete') {
            $.ajax({
                url: base_url + 'media/delete',
                type: 'POST',
                data: $(form).serializeArray(),
                dataType: 'json',
                headers: {'X-Requested-With': 'XMLHttpRequest'}
            }).done(function (data) {
                //show a message that I deleted the item, or do some fancy form.fadeOut() stuff
                update_csrf_fields(data.csrf_token); ////////TADA! This is where its AT!
            }).fail(function () {
                alert('Ajax Submit Failed ...');
            });
        }

        if (choice[0].className === 'save') {
            //a different submit button was pressed with save class
            //it will now do save actions with ajax
        }
    }
});
The controller ('media/delete'):

PHP Code:
public function delete()
{
        if ($this->request->isAJAX())
        {
            $formDataRaw $this->request->getRawInput();
            $formDataRaw['csrf_token'] = csrf_hash();
            return $this->response->setJSON($formDataRaw);
        } else {
            return '{ \'error\': \'Invalid Request\'}';
        }

You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply


Messages In This Thread
CSRF regenerate with AJAX - by Leo - 04-14-2020, 03:40 PM
RE: CSRF regenerate with AJAX - by Gary - 04-14-2020, 06:49 PM
RE: CSRF regenerate with AJAX - by Leo - 04-14-2020, 11:34 PM
RE: CSRF regenerate with AJAX - by entis - 04-15-2020, 08:02 AM
RE: CSRF regenerate with AJAX - by Gary - 04-15-2020, 12:09 PM
RE: CSRF regenerate with AJAX - by Leo - 04-15-2020, 12:30 PM
RE: CSRF regenerate with AJAX - by Morgun_Andrey - 05-05-2020, 02:39 PM
RE: CSRF regenerate with AJAX - by Gary - 04-15-2020, 01:17 PM
RE: CSRF regenerate with AJAX - by 007basaran - 02-06-2023, 08:46 PM
RE: CSRF regenerate with AJAX - by SubrataJ - 02-06-2023, 10:44 PM
RE: CSRF regenerate with AJAX - by Leo - 02-07-2023, 01:06 PM
RE: CSRF regenerate with AJAX - by Gary - 02-26-2023, 10:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB