From 0334515a43e3ccaf19c86f6e0b7b595ea260bf57 Mon Sep 17 00:00:00 2001 From: Axolotle Date: Fri, 10 Jul 2020 18:40:28 +0200 Subject: [PATCH] add temp basic api login and logout methods --- app/src/helpers/api.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/src/helpers/api.js diff --git a/app/src/helpers/api.js b/app/src/helpers/api.js new file mode 100644 index 00000000..6e2bfca7 --- /dev/null +++ b/app/src/helpers/api.js @@ -0,0 +1,38 @@ + + +function objectToParams(object) { + const urlParams = new URLSearchParams(); + for (const [key, value] of Object.entries(object)) { + urlParams.append(key, value) + } + return urlParams +} + + +export default { + options: { + credentials: 'include', + mode: 'cors', + headers: { + // FIXME is it important to keep this previous `Accept` header ? + // 'Accept': 'application/json, text/javascript, */*; q=0.01', + // Auto header is : + // "Accept": "*/*", + + // Also is this still important ? (needed by back-end) + 'X-Requested-With': 'XMLHttpRequest', + } + }, + + login(password) { + return fetch('/api/login', { + method: 'POST', + body: objectToParams({password}), + ...this.options + }).then(response => (response.ok)) + }, + + logout() { + return fetch('/api/logout', this.options).then(response => (response.ok)) + } +}