Last active
July 21, 2020 19:04
-
-
Save tachyondecay/3f3677f7236795d2de1a5a510c96f5cd to your computer and use it in GitHub Desktop.
Refresh CSRF token after 1 hour
This file contains 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
@bp.route('/csrf/') | |
@login_required | |
def get_csrf(): | |
return csrf.generate_csrf() | |
This file contains 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
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