diff --git a/src/Components/HelperFunctions/getAxiosConfig.js b/src/Components/HelperFunctions/getAxiosConfig.js index f81cfbbd5a721a4bea846bdbe7f64d5e9bebd4c7..d7980b41b31b321870674cc5bd6e024f878932f3 100644 --- a/src/Components/HelperFunctions/getAxiosConfig.js +++ b/src/Components/HelperFunctions/getAxiosConfig.js @@ -242,37 +242,57 @@ export const validateGoogleLoginToken = (url, config, onSuccess, onError) => { }) } -export const authentication = (url, payload, onSuccess, onError) => { +export async function authentication (url, payload, onSuccess, onError) { let formData = new FormData() for (const [key, value] of Object.entries(payload)) { formData.append(`${key}`,value); } - fetch((`${apiUrl}${url}`), { + let response = await fetch((`${apiUrl}${url}`), { method : 'POST', body: formData }) - .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" - } + 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)) + 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) + 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) + // }) + }