Skip to content

Commit

Permalink
update csrf token on post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-buhler committed Apr 17, 2024
1 parent 882ec4a commit 084e286
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion frontend/src/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function getCookie(name) {
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');

let csrftoken = getCookie('csrftoken');
axios.defaults.headers.post['X-CSRFToken'] = csrftoken;
export default
class APIClient {
Expand Down Expand Up @@ -145,6 +146,7 @@ export default
* @returns {Object} {user_is_correct: Boolean, right_answer: String, answer_sent: String}
*/
static async postAnswerText(answer_text) {
csrftoken = getCookie('csrftoken');
const response = await axios.post(`/api/question/answer_text/`, {
answer: answer_text,
});
Expand All @@ -157,6 +159,7 @@ export default
* @returns {Object} {user_is_correct: Boolean, right_answer: String, answer_sent: String}
*/
static async postAnswerOption(option_id) {
csrftoken = getCookie('csrftoken');
const response = await axios.post(`/api/question/answer_option/`, {
answer: option_id,
});
Expand All @@ -170,6 +173,7 @@ export default
* @returns {Object} {"message": String}
*/
static async registerUser(username, password) {
csrftoken = getCookie('csrftoken');
try {
const response = await axios.post('/api/user/register/', {
username,
Expand All @@ -188,6 +192,7 @@ export default
* @returns {Object} {"message": String}
*/
static async loginUser(username, password) {
csrftoken = getCookie('csrftoken');
try {
const response = await axios.post('/api/user/login/', {
username,
Expand All @@ -206,6 +211,7 @@ export default
* @returns {Object} {"text": String, "category": String }
*/
static async postNewCommunityQuestion(question, options) {
csrftoken = getCookie('csrftoken');
const
response = await axios.post(`/api/question/new_community/`, {
question,
Expand All @@ -220,6 +226,7 @@ export default
* @returns {Object} The response data from the API
*/
static async logOutUser() {
csrftoken = getCookie('csrftoken');
try {
const response = await axios.post('/api/user/logout/',);
return response.data;
Expand Down

0 comments on commit 084e286

Please sign in to comment.