diff --git a/src/client.js b/src/client.js index 9ee7b03..99e603c 100644 --- a/src/client.js +++ b/src/client.js @@ -1,16 +1,14 @@ +import axios from "axios"; + +axios.defaults.xsrfCookieName = 'csrftoken'; +axios.defaults.xsrfHeaderName = 'X-CSRFToken'; +axios.defaults.withCredentials = true; /** * return the token from response * * @param {Response} response - The reponse */ -function extract_csrf_token(response) -{ - let cookies = response.headers.get("set-cookie"); - csrf_token = cookies.slice(cookies.indexOf("=") + 1, cookies.indexOf(';')) - return csrf_token; -} - class Client { /** @@ -22,24 +20,14 @@ class Client constructor(url) { /** - * The api token to be logged - * @type string */ - this.token = undefined; - - /** - * The django csrftoken to send post data - * @type string */ - this.csrf_token = undefined; - - /** - * The django csrfmiddlewaretoken to send post data - * @type string */ - this.csrfmiddlewaretoken = undefined - - /** - * The api url - * @type string */ - this.url = url; + * The axios client + * @type AxiosInstance */ + this.client = axios.create({ + baseURL: url, + headers: { + 'Content-Type': 'application/json' + } + }); } /** @@ -57,9 +45,7 @@ class Client */ async _get(uri) { - let url = this.url + uri - let response = await fetch(url); - this.csrf_token = extract_csrf_token(response); + let response = await this.client.get(uri); return response; } @@ -71,20 +57,10 @@ class Client async _post(uri, data) { /** - * @type string */ - let url = this.url + uri - if (this.csrf_token === undefined) - { - let response = await fetch(url); - this.csrf_token = extract_csrf_token(response); - } - let response = await fetch(url, { - method: "POST", - headers: { - 'Referer': url, - }, - body: objectToFormData(data), - }); + * The axios client + * @type AxiosInstance */ + console.log(data) + let response = await this.client.post(uri, data) console.log(response.status) return response; } @@ -97,7 +73,7 @@ class Client */ async login(username, password) { - let response = this._post(accounts_login, {'username': username, 'password': password}) + let response = this._post(accounts_login, {username: username, password: password}) return response; } } @@ -108,7 +84,6 @@ let url = "http://0.0.0.0:8000/" let client = new Client(url) -await client._get(accounts_login) -await client.login("bozoman", "man") +let response = await client.login("bozoman", "man") export {Client} \ No newline at end of file diff --git a/src/urls.js b/src/urls.js index 64f21b3..a54f7cc 100644 --- a/src/urls.js +++ b/src/urls.js @@ -1,4 +1,4 @@ -let api = "api/" +let api = "" let accounts = api + "accounts/" let accounts_register = accounts + "register"