From 6ffbbc11de7638aca2e4ca2ed75c33b2d9733ea9 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 29 Apr 2019 23:00:55 +0000 Subject: [PATCH] --- ayanova/src/api/auth.js | 10 ++ ayanova/src/api/authutil.js | 82 +++++++----- ayanova/src/api/initialize.js | 229 ++++++++++++++++++---------------- 3 files changed, 176 insertions(+), 145 deletions(-) diff --git a/ayanova/src/api/auth.js b/ayanova/src/api/auth.js index 83477f15..19c1ef61 100644 --- a/ayanova/src/api/auth.js +++ b/ayanova/src/api/auth.js @@ -13,7 +13,17 @@ export default { ) .then(apiUtil.status) .then(apiUtil.json) + .then(result => { + /* eslint-disable-next-line */ + console.log("auth.js about to process login..."); + return result; + }) .then(processLogin) + .then(result => { + /* eslint-disable-next-line */ + console.log("auth.js returned from process login, resolving next"); + return result; + }) .then(() => { return Promise.resolve(true); }) //succeeded, nothing to return diff --git a/ayanova/src/api/authutil.js b/ayanova/src/api/authutil.js index 437b07cb..5aa00343 100644 --- a/ayanova/src/api/authutil.js +++ b/ayanova/src/api/authutil.js @@ -3,45 +3,59 @@ import decode from "jwt-decode"; import store from "../store"; 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) { - //is token present? - if (!response || !response.data || !response.data.token) { - store.commit("logItem", "auth::processLogin -> response empty"); - return Promise.reject(); - } - const token = decode(response.data.token); + var promise = new Promise(function(resolve, reject) { + //is token present? + if (!response || !response.data || !response.data.token) { + store.commit("logItem", "auth::processLogin -> response empty"); + return reject(); + } + const token = decode(response.data.token); - if (!token || !token.iss) { - store.commit("logItem", "auth::processLogin -> response token empty"); - return Promise.reject(); - } + if (!token || !token.iss) { + store.commit("logItem", "auth::processLogin -> response token empty"); + return reject(); + } - if (token.iss != "ayanova.com") { - store.commit( - "logItem", - "auth::processLogin -> token invalid (iss): " + token.iss - ); - return Promise.reject(); - } + if (token.iss != "ayanova.com") { + store.commit( + "logItem", + "auth::processLogin -> token invalid (iss): " + token.iss + ); + return reject(); + } - //Put app relevant items into vuex store so app can use them - store.commit("login", { - apiToken: response.data.token, - authenticated: true, - userId: Number(token.id), - roles: token["ayanova/roles"] + //Put app relevant items into vuex store so app can use them + store.commit("login", { + apiToken: response.data.token, + authenticated: true, + userId: Number(token.id), + 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); + }); }); - - /* 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); + return promise; } export function processLogout() { diff --git a/ayanova/src/api/initialize.js b/ayanova/src/api/initialize.js index 6f2d3775..32eea581 100644 --- a/ayanova/src/api/initialize.js +++ b/ayanova/src/api/initialize.js @@ -16,117 +16,124 @@ function addNavItem(title, icon, route) { // Initialize the app // on change of authentication status export default function initialize() { - /* eslint-disable-next-line */ - console.log("STEP 3 - INITIALIZE TOP"); - if (store.state.authenticated) { + var promise = new Promise(function(resolve) { /* eslint-disable-next-line */ - console.log("STEP 4 - INITIALIZE FETCHING LOCALE KEYS"); - //Fetch the core localized text keys that will always be required by user - locale - .fetch(locale.coreKeys) - .then(function putFetchedNavItemsInStore() { - /* eslint-disable-next-line */ - console.log("STEP 5 - DONE FETCHING LOCALE KEYS CREATING NAV ITEMS"); - //put nav items into store - //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; + console.log("STEP 3 - INITIALIZE TOP"); + if (store.state.authenticated) { + /* eslint-disable-next-line */ + console.log("STEP 4 - INITIALIZE FETCHING LOCALE KEYS"); + //Fetch the core localized text keys that will always be required by user + locale + .fetch(locale.coreKeys) + .then(function putFetchedNavItemsInStore() { /* eslint-disable-next-line */ - console.log("STEP 7 - DONE FETCHING LOCALE SETTINGS"); - } - }) - .catch(function handleFetchUserOptionsError(error) { - store.commit( - "logItem", - "Initialize::() fetch useroptions -> error" + error - ); - throw error; - }); - } + console.log("STEP 5 - DONE FETCHING LOCALE KEYS CREATING NAV ITEMS"); + //put nav items into store + //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"); + }) + .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; }