mardi 8 mars 2016

TokenMismatchException during AJAX polling in Laravel

In my Laravel application, I have a notification system. This is run using AJAX polling techniques (i.e. a ajax post function is run every 5 seconds or so) similar to:

$.ajaxSetup({
    headers: {
        'X-CSRF-Token': CSRF_TOKEN
    }
});

(function pollForNewNotifications() {
    setTimeout(function () {
        $.ajax({
                type: 'POST',
                url: 'http://ift.tt/1R5jr1A',
                dataType: 'json',
                data: {
                    // data that is sent
                },
                success: function (data) {
                    // add new notifications if data is not empty logic
                    pollForNewNotifications();
                }
            })
            .fail(function (xhr, status, err) {
                console.error(xhr.responseText)
            });
    }, 5000);
})();

Now this works most of the time.

However, the issue is that I sometimes find that over long period of inactivity I get a TokenMismatchException in VerifyCsrfToken.php error and automatically logged out.

I believe that this is the case because the CSRF token changes or longer is valid (I may be wrong).

How can I solve this issue?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire