From 087f7110139ed0d48efb8ca315d53f30863fc551 Mon Sep 17 00:00:00 2001 From: Luis Felipe Risch <lfr20@inf.ufpr.br> Date: Fri, 19 Feb 2021 10:08:56 -0300 Subject: [PATCH] Copy and paste from 'develop' --- .../HelperFunctions/getAxiosConfig.js | 139 ++++++++++++------ 1 file changed, 90 insertions(+), 49 deletions(-) diff --git a/src/Components/HelperFunctions/getAxiosConfig.js b/src/Components/HelperFunctions/getAxiosConfig.js index 7841d441..d7980b41 100644 --- a/src/Components/HelperFunctions/getAxiosConfig.js +++ b/src/Components/HelperFunctions/getAxiosConfig.js @@ -1,5 +1,4 @@ import {apiUrl} from '../../env.js' -import axios from 'axios' export function getAxiosConfigFromJSON () { let config = { @@ -79,25 +78,39 @@ function updateAccessToken (newAccessToken) { } } -export const getRequest = (url, onSuccess, onError) => { - fetch((`${apiUrl}${url}`), { +export async function getRequest (url, onSuccess, onError) { + let response = await fetch((`${apiUrl}${url}`), { headers : fetchHeaders() }) - .then(response => { - if (response.headers.has('access-token')) { - updateAccessToken(response.headers.get('access-token')) - } - return response.json().catch(err => { - return {}; - }) - }) - .then(data => { - console.log(data) - onSuccess(data) - }) - .catch(error => { - onError(error) - }) + if (response.ok) { + if (response.headers.has('access-token')) { + updateAccessToken(response.headers.get('access-token')) + } + + let json = await response.json() + onSuccess(json, response.headers) + } + else { + onError(response.error) + } + // .then(response => { + // if (response.headers.has('access-token')) { + // updateAccessToken(response.headers.get('access-token')) + // } + // let json = await response.json() + // + // onSuccess(json) + // + // return response.json().catch(err => { + // return {}; + // }) + // }) + // .then(data => { + // onSuccess(data) + // }) + // .catch(error => { + // onError(error) + // }) } export const deleteRequest = (url, onSuccess, onError) => { @@ -114,7 +127,6 @@ export const deleteRequest = (url, onSuccess, onError) => { }) }) .then(data => { - console.log(data) onSuccess(data) }) .catch(error => { @@ -124,10 +136,7 @@ export const deleteRequest = (url, onSuccess, onError) => { export const putRequest = (url, payload, onSuccess, onError) => { let newHeaders = fetchHeaders() - if (payload instanceof FormData) { - newHeaders.append('Content-Type', 'multipart/form-data') - } - else { + if (!(payload instanceof FormData)) { newHeaders.append('Content-Type', 'application/json') } @@ -145,7 +154,6 @@ export const putRequest = (url, payload, onSuccess, onError) => { }) }) .then(data => { - console.log(data) onSuccess(data) }) .catch(error => { @@ -176,7 +184,6 @@ export const postRequest = (url, payload, onSuccess, onError) => { }) }) .then(data => { - console.log(data) onSuccess(data) }) .catch(error => { @@ -189,9 +196,7 @@ export const fetchAllRequest = (urls, onSuccess, onError) => { headers : fetchHeaders() }))).then( (responses) => { for(let res of responses) { - console.log(res) if (res.headers.has('access-token') && res.status !== 304) { - console.log(res) updateAccessToken(res.headers.get('access-token')) } } @@ -205,31 +210,13 @@ export const fetchAllRequest = (urls, onSuccess, onError) => { }) } -export const validateGoogleLoginToken = (config, onSuccess, onError) => { - axios.get( (`${apiUrl}/auth/validate_token/`), config ).then( - (response) => { +export const validateGoogleLoginToken = (url, config, onSuccess, onError) => { + const newHeaders = new Headers(config.headers) - if ( response.headers['access-token'] ) { - updateHeaders(response.headers) - } - - onSuccess(response.data) - }, - (error) => { - onError(error) - } - ) -} - -export const authentication = (url, payload, onSuccess, onError) => { - let formData = new FormData() - for (const [key, value] of Object.entries(payload)) { - formData.append(`${key}`,value); - } fetch((`${apiUrl}${url}`), { - method : 'POST', - body: formData + method : 'GET', + headers : newHeaders }) .then(response => { const auth_headers = { @@ -253,5 +240,59 @@ export const authentication = (url, payload, onSuccess, onError) => { .catch(error => { onError(error) }) +} + +export async function authentication (url, payload, onSuccess, onError) { + let formData = new FormData() + for (const [key, value] of Object.entries(payload)) { + formData.append(`${key}`,value); + } + + let response = await fetch((`${apiUrl}${url}`), { + method : 'POST', + body: formData + }) + if (response.ok) { + const auth_headers = { + client: response.headers.get('client'), + "access-token": response.headers.get('access-token'), + uid: response.headers.get('uid'), + expiry: response.headers.get('expiry'), + "token-type": "Bearer" + } + + sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(auth_headers)) + + let json = await response.json().catch(err => { + return {}; + }) + + onSuccess(json) + } + else { + onError(response.error) + } + // .then(response => { + // const auth_headers = { + // client: response.headers.get('client'), + // "access-token": response.headers.get('access-token'), + // uid: response.headers.get('uid'), + // expiry: response.headers.get('expiry'), + // "token-type": "Bearer" + // } + // + // sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(auth_headers)) + // + // return response.json().catch(err => { + // return {}; + // }) + // }) + // .then(data => { + // console.log(data) + // onSuccess(data) + // }) + // .catch(error => { + // onError(error) + // }) } -- GitLab