Skip to content

Instantly share code, notes, and snippets.

@tachyondecay
Last active July 21, 2020 19:04
Show Gist options
  • Save tachyondecay/3f3677f7236795d2de1a5a510c96f5cd to your computer and use it in GitHub Desktop.
Save tachyondecay/3f3677f7236795d2de1a5a510c96f5cd to your computer and use it in GitHub Desktop.
Refresh CSRF token after 1 hour
@bp.route('/csrf/')
@login_required
def get_csrf():
return csrf.generate_csrf()
async function refreshCSRF() {
const input = document.querySelectorAll('[name=csrf_token]');
if(input) {
const response = await fetch('/api/csrf/');
data = await response.text();
if(response.ok) {
csrf_token = data; // csrf_token is a global variable I inject using a <script> tag.
input.forEach(el => {
el.value = data;
});
console.log('New token ' + data);
} else {
console.log('Could not fetch CSRF token: ' + data);
}
}
}
let prevent_csrf_expiry = setInterval(refreshCSRF, 1000 * 3600);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment