This commit is contained in:
2018-11-06 23:54:47 +00:00
parent 3c0f696c68
commit dadeb45342
5 changed files with 173 additions and 97 deletions

View File

@@ -1,24 +1,74 @@
/*eslint-disable*/
import ayconfig from "../utils/config";
function status(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
return Promise.reject(new Error(response.statusText));
}
}
function json(response) {
var v = response.json();
return v;
}
export default {
authenticate(login, password) {
fetch(ayconfig.apiUrl + "/auth", {
login: login,
password: password
})
.then(function(response) {
if (response.status != 200) {
alert("Error: " + response.statusText);
return;
}
// Handle response you get from the server
response.json().then(function(data) {
//set token in config here
return data;
});
authenticate(login, password, cb) {
fetch(ayconfig.apiUrl + "auth", {
method: "post",
mode: "cors",
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
},
body: JSON.stringify({
login: login,
password: password
})
.catch(function(err) {
alert("Fetch Error :-S", err);
})
.then(status)
.then(json)
.then(function(data) {
console.log(
"AUTH.JS::authenticate() -> Request succeeded with JSON response",
data
);
cb(data);
//return data;
})
.catch(function(error) {
console.log("Request failed", error);
cb(error); //sb cb(data,error or something)
//return error;
});
},
async authenticatepromise(login, password) {
return fetch(ayconfig.apiUrl + "auth", {
method: "post",
mode: "cors",
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
},
body: JSON.stringify({
login: login,
password: password
})
})
.then(status)
.then(json)
.then(function(data) {
console.log(
"AUTH.JS::authenticatepromise() -> Request succeeded with JSON response",
data
);
return data;
})
.catch(function(error) {
console.log("Request failed", error);
return error;
});
}
};