Skip to content
Snippets Groups Projects
Commit 976a83f3 authored by Lucas Eduardo Schoenfelder's avatar Lucas Eduardo Schoenfelder
Browse files

changed login function to async to better handle errors

parent c508d0ec
No related branches found
No related tags found
3 merge requests!57Merge of develop into master,!56Fixed buttons reportar, seguir, compartilhar, guardar and entrar (in comments...,!39Update admin system
......@@ -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)
// })
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment