diff --git a/src/shared/apiService.js b/src/shared/apiService.js index 9beb0320..475e8a20 100644 --- a/src/shared/apiService.js +++ b/src/shared/apiService.js @@ -1,5 +1,6 @@ import axios from 'axios'; -import { apiBase } from '../store/modules/config'; + +export const apiBase = '/api/v1'; export const apiStore = { store: null }; @@ -37,3 +38,16 @@ api.interceptors.response.use( // // return Promise.reject(err); }, ); + + +export function setBaseUrl(baseURL) { + if (baseURL === null) { + // Reset to "local" baseurl + api.defaults.baseURL = apiBase; + } else if (!baseURL.endsWith(apiBase)) { + api.defaults.baseURL = `${baseURL}${apiBase}`; + } else { + api.defaults.baseURL = `${baseURL}`; + } + // Do some more testing here ? +} diff --git a/src/store/index.js b/src/store/index.js index 1f9d3036..0d252128 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,13 +1,10 @@ import Vue from 'vue'; import Vuex from 'vuex'; -import axios from 'axios'; import ftbotModule from './modules/ftbot'; import userModule from './modules/user'; import alertsModule from './modules/alerts'; -import { apiBase } from './modules/config'; - Vue.use(Vuex); export default new Vuex.Store({ @@ -25,12 +22,6 @@ export default new Vuex.Store({ }, }, actions: { - ping({ commit }) { - axios - .get(`${apiBase}/ping`) - .then((result) => commit('setPing', result.data)) - .catch(console.error); - }, refreshAll({ dispatch }) { dispatch('refreshFrequent'); dispatch('refreshSlow'); diff --git a/src/store/modules/config.js b/src/store/modules/config.js deleted file mode 100644 index 08451b19..00000000 --- a/src/store/modules/config.js +++ /dev/null @@ -1,5 +0,0 @@ -const apiBase = '/api/v1'; - -module.exports = { - apiBase, -}; diff --git a/src/store/modules/ftbot.js b/src/store/modules/ftbot.js index cd7be6ac..e1362d9e 100644 --- a/src/store/modules/ftbot.js +++ b/src/store/modules/ftbot.js @@ -1,4 +1,4 @@ -import { api } from '../../shared/apiService'; +import { api } from '@/shared/apiService'; export default { namespaced: true, @@ -54,6 +54,12 @@ export default { }, }, actions: { + ping({ commit }) { + api + .get('/ping') + .then((result) => commit('setPing', result.data)) + .catch(console.error); + }, getTrades({ commit }) { console.log('fetching trades'); return api diff --git a/src/store/modules/user.js b/src/store/modules/user.js index e85623f8..c7223a05 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,12 +1,16 @@ import axios from 'axios'; -import { apiBase } from './config'; +import { apiBase, setBaseUrl } from '@/shared/apiService'; const AUTH_REF_TOKEN = 'auth_ref_token'; const AUTH_ACCESS_TOKEN = 'auth_access_token'; +const AUTH_API_URL = 'auth_api_url'; const initAccessToken = JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN)); +// Construct the api as "inserted URL" + /api/v1 +setBaseUrl(`${JSON.parse(localStorage.getItem(AUTH_API_URL))}`); + export default { namespaced: true, state: { @@ -28,6 +32,10 @@ export default { }, }, mutations: { + setAPIUrl(state, apiurl) { + localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl)); + setBaseUrl(apiurl); + }, setAccessTokens(state, token) { localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token)); state.accessToken = token; @@ -45,18 +53,21 @@ export default { console.log('Logging out'); localStorage.removeItem(AUTH_REF_TOKEN); localStorage.removeItem(AUTH_ACCESS_TOKEN); + localStorage.removeItem(AUTH_API_URL); state.auth = null; state.loggedIn = false; + setBaseUrl(null); }, }, actions: { login({ commit }, auth) { + commit('setAPIUrl', auth.url); // Login using username / password console.log(auth); axios .post( - `${apiBase}/token/login`, + `${auth.url}${apiBase}/token/login`, {}, { auth: { ...auth }, diff --git a/src/views/Login.vue b/src/views/Login.vue index e7869f41..28ec852a 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -10,10 +10,24 @@ @ok="handleOk" >
+ + +