Last active
June 23, 2020 08:45
-
-
Save tanthammar/88c7a28b68573f70dc3d09abb26d454b to your computer and use it in GitHub Desktop.
Keep livewire form session alive (419)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script data-turbolinks-eval=false> | |
function updateToken() { | |
fetch('/update-csrf') | |
.then(response => response.text()) | |
.then(csrf => { | |
document.head.querySelector('meta[name="csrf-token"]').setAttribute('content', csrf) | |
}) | |
} | |
setInterval(updateToken, 1000 * 60 * 5) | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('update-csrf', fn () => response(csrf_token()))->middleware('auth'); |
Here is an improved version, without csrf route
https://gist.github.com/tanthammar/02c615e73022c44dda6533ed9416ac29
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
redirect if logged out
Or if you do not want to return the csrf you could just check if the user is authenticated. The session token will be the same as long as the user is logged in. By polling the server the session is kept alive.
This one covers the situation where the window has lost its focus, the user will be redirected when they get back to the window.