Skip to content

Instantly share code, notes, and snippets.

@ohnotnow
Created May 24, 2018 14:33
Show Gist options
  • Save ohnotnow/f4f54fd5d0a68aaf4ee291a13c7ab2ae to your computer and use it in GitHub Desktop.
Save ohnotnow/f4f54fd5d0a68aaf4ee291a13c7ab2ae to your computer and use it in GitHub Desktop.
csrf hackyness
document.addEventListener("DOMContentLoaded", function () {
setInterval(keepTokenAlive, 1000 * 60 * 15); // every 15 mins
function keepTokenAlive() {
axios.get('/keep-token-alive')
.then(response => {
let token = response.data.token;
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = response.data.token;
const formFields = document.querySelectorAll('input[name="_token"]');
Array.from(formFields).forEach(field => {
field.value = token;
});
})
.catch(error => {
console.log(error);
});
}
});
Auth::routes();
Route::get('/keep-token-alive', function () {
return ['token' => csrf_token()];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment