I'm working on an app with Vue frontend and Flask backend.
I am writing my forms in Vue but I'm trying to use FlaskWTF for CSRF/XSRF security and for form validation on the back-end.
I've wrapped my app in CSRFProtect() as they say to do in the FlaskWTF docs, if using AJAX.
from flask_wtf.csrf import CSRFProtect
app = build_app()
csrf = CSRFProtect(app)
But I'm stuck on how to send over the CSRF token in a form that is readable for Vue. In their docs they suggest rendering a template with the following script:
<form method="post">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
</form>
And then settings headers on future post request with the following code:
<script type="text/javascript">
var csrf_token = "{{ csrf_token() }}";
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
}
});
</script>
However, I'm not using jQuery, and I'm stuck on how to even access the token when it's sent as a giant script tag in my response payload (see more details on this problem in this question that I posted).
With all that in mind -- what is the best practice for handling CSRF using FlaskWTF+Vue? How should I be sending over my CSRF token and how should I be receiving it with Vue?