This commit is contained in:
2019-04-29 23:00:55 +00:00
parent aa3751be35
commit 6ffbbc11de
3 changed files with 176 additions and 145 deletions

View File

@@ -13,7 +13,17 @@ export default {
) )
.then(apiUtil.status) .then(apiUtil.status)
.then(apiUtil.json) .then(apiUtil.json)
.then(result => {
/* eslint-disable-next-line */
console.log("auth.js about to process login...");
return result;
})
.then(processLogin) .then(processLogin)
.then(result => {
/* eslint-disable-next-line */
console.log("auth.js returned from process login, resolving next");
return result;
})
.then(() => { .then(() => {
return Promise.resolve(true); return Promise.resolve(true);
}) //succeeded, nothing to return }) //succeeded, nothing to return

View File

@@ -3,45 +3,59 @@ import decode from "jwt-decode";
import store from "../store"; import store from "../store";
import initialize from "./initialize"; import initialize from "./initialize";
// var secondMethod = function(someStuff) {
// var promise = new Promise(function(resolve, reject) {
// setTimeout(function() {
// console.log("second method completed");
// resolve({ newData: someStuff.data + " some more data" });
// }, 2000);
// });
// return promise;
// };
export function processLogin(response) { export function processLogin(response) {
//is token present? var promise = new Promise(function(resolve, reject) {
if (!response || !response.data || !response.data.token) { //is token present?
store.commit("logItem", "auth::processLogin -> response empty"); if (!response || !response.data || !response.data.token) {
return Promise.reject(); store.commit("logItem", "auth::processLogin -> response empty");
} return reject();
const token = decode(response.data.token); }
const token = decode(response.data.token);
if (!token || !token.iss) { if (!token || !token.iss) {
store.commit("logItem", "auth::processLogin -> response token empty"); store.commit("logItem", "auth::processLogin -> response token empty");
return Promise.reject(); return reject();
} }
if (token.iss != "ayanova.com") { if (token.iss != "ayanova.com") {
store.commit( store.commit(
"logItem", "logItem",
"auth::processLogin -> token invalid (iss): " + token.iss "auth::processLogin -> token invalid (iss): " + token.iss
); );
return Promise.reject(); return reject();
} }
//Put app relevant items into vuex store so app can use them //Put app relevant items into vuex store so app can use them
store.commit("login", { store.commit("login", {
apiToken: response.data.token, apiToken: response.data.token,
authenticated: true, authenticated: true,
userId: Number(token.id), userId: Number(token.id),
roles: token["ayanova/roles"] roles: token["ayanova/roles"]
});
/* eslint-disable-next-line */
console.log("STEP 2 - PROCESS LOGIN - CALLING INITIALIZE");
//Initialize the application
initialize().then(() => {
store.commit(
"logItem",
"auth::processLogin -> User " + token.id + " logged in"
);
resolve(true);
});
}); });
return promise;
/* eslint-disable-next-line */
console.log("STEP 2 - PROCESS LOGIN - CALILNG INITIALIZE");
//Initialize the application
initialize();
store.commit(
"logItem",
"auth::processLogin -> User " + token.id + " logged in"
);
return Promise.resolve(true);
} }
export function processLogout() { export function processLogout() {

View File

@@ -16,117 +16,124 @@ function addNavItem(title, icon, route) {
// Initialize the app // Initialize the app
// on change of authentication status // on change of authentication status
export default function initialize() { export default function initialize() {
/* eslint-disable-next-line */ var promise = new Promise(function(resolve) {
console.log("STEP 3 - INITIALIZE TOP");
if (store.state.authenticated) {
/* eslint-disable-next-line */ /* eslint-disable-next-line */
console.log("STEP 4 - INITIALIZE FETCHING LOCALE KEYS"); console.log("STEP 3 - INITIALIZE TOP");
//Fetch the core localized text keys that will always be required by user if (store.state.authenticated) {
locale /* eslint-disable-next-line */
.fetch(locale.coreKeys) console.log("STEP 4 - INITIALIZE FETCHING LOCALE KEYS");
.then(function putFetchedNavItemsInStore() { //Fetch the core localized text keys that will always be required by user
/* eslint-disable-next-line */ locale
console.log("STEP 5 - DONE FETCHING LOCALE KEYS CREATING NAV ITEMS"); .fetch(locale.coreKeys)
//put nav items into store .then(function putFetchedNavItemsInStore() {
//Everyone has a home
addNavItem(locale.get("Home"), "home", "/");
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.TechLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.TechFull) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.SubContractorLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.SubContractorFull)
) {
addNavItem(locale.get("Service"), "toolbox", "/service");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.DispatchLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.DispatchFull)
) {
addNavItem(locale.get("Dispatch"), "shipping-fast", "/dispatch");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.InventoryLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.InventoryFull)
) {
addNavItem(locale.get("Inventory"), "dolly", "/inventory");
}
if (roles.hasRole(roles.AUTHORIZATION_ROLES.AccountingFull)) {
addNavItem(
locale.get("Accounting"),
"file-invoice-dollar",
"/accounting"
);
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.BizAdminLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.BizAdminFull)
) {
addNavItem(locale.get("Administration"), "user-tie", "/admin");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.OpsAdminFull) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.OpsAdminLimited)
) {
addNavItem(locale.get("Operations"), "cogs", "ops");
}
//MOVED TO MENU OUT OF NAV
//Everyone can see about and logout
// addNavItem(locale.get("HelpAboutAyaNova"), "info-circle", "/about");
// addNavItem(locale.get("Logout"), "sign-out-alt", "/login");
})
.catch(function handleIntializeError(error) {
store.commit("logItem", "Initialize::() ltfetch -> error" + error);
throw error;
});
/* eslint-disable-next-line */
console.log("STEP 6 - INIT DONE WITH NAV, NOW FETCHING USEROPTIONS SETTINGS");
//CACHE LOCALE SETTINGS
api
.get("UserOptions/" + store.state.userId)
.then(res => {
if (res.error) {
//In a form this would trigger a bunch of validation or error display code but for here and now:
//convert error to human readable string for display
alert(api.apiErrorToHumanString(res.error));
} else {
//TODO: also need the other locale settings such as number and date formats etc
var localOffset = new Date().getTimezoneOffset();
if (localOffset != 0) {
localOffset = (localOffset / 60) * -1; //time is in minutes and reversed from what we want or expect
}
if (res.data.timeZoneOffset != localOffset) {
//todo: timezone doesn't match, offer to fix it
// alert(
// "Time zone offset for this account is set to " +
// res.data.timeZoneOffset +
// " which doesn't match the local timezone offset of " +
// localOffset +
// "."
// );
}
//Store offset in locale data
locale.timeZoneOffset = res.data.timeZoneOffset;
/* eslint-disable-next-line */ /* eslint-disable-next-line */
console.log("STEP 7 - DONE FETCHING LOCALE SETTINGS"); console.log("STEP 5 - DONE FETCHING LOCALE KEYS CREATING NAV ITEMS");
} //put nav items into store
}) //Everyone has a home
.catch(function handleFetchUserOptionsError(error) { addNavItem(locale.get("Home"), "home", "/");
store.commit(
"logItem", if (
"Initialize::() fetch useroptions -> error" + error roles.hasRole(roles.AUTHORIZATION_ROLES.TechLimited) ||
); roles.hasRole(roles.AUTHORIZATION_ROLES.TechFull) ||
throw error; roles.hasRole(roles.AUTHORIZATION_ROLES.SubContractorLimited) ||
}); roles.hasRole(roles.AUTHORIZATION_ROLES.SubContractorFull)
} ) {
addNavItem(locale.get("Service"), "toolbox", "/service");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.DispatchLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.DispatchFull)
) {
addNavItem(locale.get("Dispatch"), "shipping-fast", "/dispatch");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.InventoryLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.InventoryFull)
) {
addNavItem(locale.get("Inventory"), "dolly", "/inventory");
}
if (roles.hasRole(roles.AUTHORIZATION_ROLES.AccountingFull)) {
addNavItem(
locale.get("Accounting"),
"file-invoice-dollar",
"/accounting"
);
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.BizAdminLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.BizAdminFull)
) {
addNavItem(locale.get("Administration"), "user-tie", "/admin");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.OpsAdminFull) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.OpsAdminLimited)
) {
addNavItem(locale.get("Operations"), "cogs", "ops");
}
//MOVED TO MENU OUT OF NAV
//Everyone can see about and logout
// addNavItem(locale.get("HelpAboutAyaNova"), "info-circle", "/about");
// addNavItem(locale.get("Logout"), "sign-out-alt", "/login");
})
.then(() => {
/* eslint-disable-next-line */
console.log(
"STEP 6 - INIT DONE WITH NAV, NOW FETCHING USEROPTIONS SETTINGS"
);
//CACHE LOCALE SETTINGS
api
.get("UserOptions/" + store.state.userId)
.then(res => {
if (res.error) {
//In a form this would trigger a bunch of validation or error display code but for here and now:
//convert error to human readable string for display
alert(api.apiErrorToHumanString(res.error));
} else {
//TODO: also need the other locale settings such as number and date formats etc
var localOffset = new Date().getTimezoneOffset();
if (localOffset != 0) {
localOffset = (localOffset / 60) * -1; //time is in minutes and reversed from what we want or expect
}
if (res.data.timeZoneOffset != localOffset) {
//todo: timezone doesn't match, offer to fix it
// alert(
// "Time zone offset for this account is set to " +
// res.data.timeZoneOffset +
// " which doesn't match the local timezone offset of " +
// localOffset +
// "."
// );
}
//Store offset in locale data
locale.timeZoneOffset = res.data.timeZoneOffset;
/* eslint-disable-next-line */
console.log("STEP 7 - DONE FETCHING LOCALE SETTINGS");
resolve();
}
})
.catch(function handleFetchUserOptionsError(error) {
store.commit(
"logItem",
"Initialize::() fetch useroptions -> error" + error
);
throw error;
});
})
.catch(function handleIntializeError(error) {
store.commit("logItem", "Initialize::() ltfetch -> error" + error);
throw error;
});
}
});
return promise;
} }