diff --git a/src/api/index.ts b/src/api/index.ts index 06b9a5a..b7c8d81 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,23 +3,15 @@ import { setupCache } from "axios-cache-adapter" import OfflineHelper from "@/offline-helper" import emitter from "@/event-bus" import store from "@/store"; -import { StatusCodes } from "http-status-codes"; -import router from "@/router" -let apiConfig = {} as any axios.interceptors.request.use((config: any) => { // TODO: pass csrf token const token = store.getters["user/getUserToken"]; - if (token && !apiConfig.useOmsRedirection) { + if (token) { config.headers["api_key"] = token; config.headers["Content-Type"] = "application/json"; } - const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"] - if (apiConfig.useOmsRedirection && omsRedirectionInfo.token) { - config.headers["Authorization"] = `Bearer ${omsRedirectionInfo.token}`; - config.headers["Content-Type"] = "application/json"; - } return config; }); @@ -51,12 +43,12 @@ axios.interceptors.response.use(function (response) { if (error.response) { // TODO Handle case for failed queue request const { status } = error.response; - if (status === StatusCodes.UNAUTHORIZED) { - store.dispatch("user/logout"); - const redirectUrl = window.location.origin + '/login'; - // Explicitly passing isLoggedOut as in case of maarg apps we need to call the logout api in launchpad - window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}&isLoggedOut=true`; - } + // if (status === StatusCodes.UNAUTHORIZED) { + // store.dispatch("user/logout"); + // const redirectUrl = window.location.origin + '/login'; + // Explicitly passing isLoggedOut as in case of maarg apps we need to call the logout api in launchpad + // window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}&isLoggedOut=true`; + // } } // Any status codes that falls outside the range of 2xx cause this function to trigger // Do something with response error @@ -85,7 +77,6 @@ const axiosCache = setupCache({ * @return {Promise} Response from API as returned by Axios */ const api = async (customConfig: any) => { - apiConfig = customConfig // Prepare configuration const config: any = { url: customConfig.url, @@ -96,11 +87,8 @@ const api = async (customConfig: any) => { } const baseURL = store.getters["user/getInstanceUrl"]; - const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"] - if(customConfig.useOmsRedirection) { - config.baseURL = omsRedirectionInfo.url.startsWith('http') ? omsRedirectionInfo.url.includes('/api') ? omsRedirectionInfo.url : `${omsRedirectionInfo.url}/api/` : `https://${omsRedirectionInfo.url}.hotwax.io/api/`; - } else if (baseURL) { + if (baseURL) { config.baseURL = baseURL.startsWith('http') ? baseURL.includes('/rest/s1/order-routing') ? baseURL : `${baseURL}/rest/s1/order-routing/` : `https://${baseURL}.hotwax.io/rest/s1/order-routing/`; } @@ -124,7 +112,7 @@ const api = async (customConfig: any) => { * @return {Promise} Response from API as returned by Axios */ const client = (config: any) => { - return axios.request(config); + return axios.create().request(config) } export { api as default, client, axios }; \ No newline at end of file diff --git a/src/services/UserService.ts b/src/services/UserService.ts index b8f7eae..40172a5 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -33,8 +33,7 @@ const login = async (token: string): Promise => { const getUserPermissions = async (payload: any, url: string, token: any): Promise => { // Currently, making this request in ofbiz - const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"] - const baseURL = omsRedirectionInfo.url.startsWith('http') ? omsRedirectionInfo.url.includes('/api') ? omsRedirectionInfo.url : `${omsRedirectionInfo.url}/api/` : `https://${omsRedirectionInfo.url}.hotwax.io/api/`; + const baseURL = url.startsWith('http') ? url.includes('/api') ? url : `${url}/api/` : `https://${url}.hotwax.io/api/`; let serverPermissions = [] as any; // If the server specific permission list doesn't exist, getting server permissions will be of no use diff --git a/src/store/modules/user/actions.ts b/src/store/modules/user/actions.ts index 0265bae..8e14d9f 100644 --- a/src/store/modules/user/actions.ts +++ b/src/store/modules/user/actions.ts @@ -30,10 +30,7 @@ const actions: ActionTree = { // Prepare permissions list const serverPermissionsFromRules = getServerPermissionsFromRules(); if (permissionId) serverPermissionsFromRules.push(permissionId); - if(omsRedirectionUrl && token) { - dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token }) - } - + const serverPermissions: Array = await UserService.getUserPermissions({ permissionIds: [...new Set(serverPermissionsFromRules)] }, omsRedirectionUrl, token); @@ -52,19 +49,22 @@ const actions: ActionTree = { return Promise.reject(new Error(permissionError)); } } - + emitter.emit("presentLoader", { message: "Logging in...", backdropDismiss: false }) const api_key = await UserService.login(token) const userProfile = await UserService.getUserProfile(api_key); - + // TODO: fetch only associated product stores for user, currently api does not support this userProfile.stores = await UserService.getEComStores(api_key); - + if (userProfile.timeZone) { Settings.defaultZone = userProfile.timeZone; } setPermissions(appPermissions); + if(omsRedirectionUrl && token) { + dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token }) + } commit(types.USER_TOKEN_CHANGED, { newToken: api_key }) commit(types.USER_INFO_UPDATED, userProfile); commit(types.USER_PERMISSIONS_UPDATED, appPermissions);