This commit is contained in:
2018-11-08 01:08:09 +00:00
parent 063b68c9b9
commit 8c7a001bca
4 changed files with 60 additions and 40 deletions

View File

@@ -1,8 +1,11 @@
import logger from "../utils/logit";
export default {
status(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
logger.log("API error", response.statusText);
return Promise.reject(new Error(response.statusText));
}
},

View File

@@ -1,37 +1,30 @@
import config from "../utils/config";
import api from "./apiutil";
import { processLogin, processLogout } from "../utils/auth";
import { processLogin, processLogout } from "../utils/authUtil";
export default {
async authenticate(login, password) {
return (
fetch(config.apiUrl + "auth", {
method: "post",
mode: "cors",
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
},
body: JSON.stringify({
login: login,
password: password
})
return fetch(config.apiUrl + "auth", {
method: "post",
mode: "cors",
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
},
body: JSON.stringify({
login: login,
password: password
})
.then(api.status)
.then(api.json)
.then(processLogin)
.then(() => {
return Promise.resolve(true);
}) //succeeded, nothing to return
// .then(function(data) {
// //todo: this should just return a bool on successful login and let the util\auth handle storing token etc
// //router will handle views available based on roles etc so login only really needs to know if it succeeded or not.
// return data;
// })
.catch(function(error) {
processLogout();
return Promise.reject(error);
})
);
})
.then(api.status)
.then(api.json)
.then(processLogin)
.then(() => {
return Promise.resolve(true);
}) //succeeded, nothing to return
.catch(function(error) {
processLogout();
return Promise.reject(error);
});
}
};