Have user input login-url

This commit is contained in:
Matthias 2020-05-20 07:19:14 +02:00
parent 37188543d8
commit 6f84b18130
6 changed files with 52 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import axios from 'axios'; import axios from 'axios';
import { apiBase } from '../store/modules/config';
export const apiBase = '/api/v1';
export const apiStore = { store: null }; export const apiStore = { store: null };
@ -37,3 +38,16 @@ api.interceptors.response.use(
// // return Promise.reject(err); // // 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 ?
}

View File

@ -1,13 +1,10 @@
import Vue from 'vue'; import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import axios from 'axios';
import ftbotModule from './modules/ftbot'; import ftbotModule from './modules/ftbot';
import userModule from './modules/user'; import userModule from './modules/user';
import alertsModule from './modules/alerts'; import alertsModule from './modules/alerts';
import { apiBase } from './modules/config';
Vue.use(Vuex); Vue.use(Vuex);
export default new Vuex.Store({ export default new Vuex.Store({
@ -25,12 +22,6 @@ export default new Vuex.Store({
}, },
}, },
actions: { actions: {
ping({ commit }) {
axios
.get(`${apiBase}/ping`)
.then((result) => commit('setPing', result.data))
.catch(console.error);
},
refreshAll({ dispatch }) { refreshAll({ dispatch }) {
dispatch('refreshFrequent'); dispatch('refreshFrequent');
dispatch('refreshSlow'); dispatch('refreshSlow');

View File

@ -1,5 +0,0 @@
const apiBase = '/api/v1';
module.exports = {
apiBase,
};

View File

@ -1,4 +1,4 @@
import { api } from '../../shared/apiService'; import { api } from '@/shared/apiService';
export default { export default {
namespaced: true, namespaced: true,
@ -54,6 +54,12 @@ export default {
}, },
}, },
actions: { actions: {
ping({ commit }) {
api
.get('/ping')
.then((result) => commit('setPing', result.data))
.catch(console.error);
},
getTrades({ commit }) { getTrades({ commit }) {
console.log('fetching trades'); console.log('fetching trades');
return api return api

View File

@ -1,12 +1,16 @@
import axios from 'axios'; import axios from 'axios';
import { apiBase } from './config'; import { apiBase, setBaseUrl } from '@/shared/apiService';
const AUTH_REF_TOKEN = 'auth_ref_token'; const AUTH_REF_TOKEN = 'auth_ref_token';
const AUTH_ACCESS_TOKEN = 'auth_access_token'; const AUTH_ACCESS_TOKEN = 'auth_access_token';
const AUTH_API_URL = 'auth_api_url';
const initAccessToken = JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN)); 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 { export default {
namespaced: true, namespaced: true,
state: { state: {
@ -28,6 +32,10 @@ export default {
}, },
}, },
mutations: { mutations: {
setAPIUrl(state, apiurl) {
localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl));
setBaseUrl(apiurl);
},
setAccessTokens(state, token) { setAccessTokens(state, token) {
localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token)); localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token));
state.accessToken = token; state.accessToken = token;
@ -45,18 +53,21 @@ export default {
console.log('Logging out'); console.log('Logging out');
localStorage.removeItem(AUTH_REF_TOKEN); localStorage.removeItem(AUTH_REF_TOKEN);
localStorage.removeItem(AUTH_ACCESS_TOKEN); localStorage.removeItem(AUTH_ACCESS_TOKEN);
localStorage.removeItem(AUTH_API_URL);
state.auth = null; state.auth = null;
state.loggedIn = false; state.loggedIn = false;
setBaseUrl(null);
}, },
}, },
actions: { actions: {
login({ commit }, auth) { login({ commit }, auth) {
commit('setAPIUrl', auth.url);
// Login using username / password // Login using username / password
console.log(auth); console.log(auth);
axios axios
.post( .post(
`${apiBase}/token/login`, `${auth.url}${apiBase}/token/login`,
{}, {},
{ {
auth: { ...auth }, auth: { ...auth },

View File

@ -10,10 +10,24 @@
@ok="handleOk" @ok="handleOk"
> >
<form ref="form" @submit.stop.prevent="handleSubmit"> <form ref="form" @submit.stop.prevent="handleSubmit">
<b-form-group
:state="urlState"
label="API Url"
label-for="url-input"
invalid-feedback="API Url required"
>
<b-form-input
id="url-input"
v-model="auth.url"
:state="nameState"
@keydown.enter.native="handleOk"
required
></b-form-input>
</b-form-group>
<b-form-group <b-form-group
:state="nameState" :state="nameState"
label="Username" label="Username"
label-for="name-input" label-for="username-input"
invalid-feedback="Name is required" invalid-feedback="Name is required"
> >
<b-form-input <b-form-input
@ -50,10 +64,12 @@ export default {
data() { data() {
return { return {
auth: { auth: {
url: 'http://localhost:8080',
username: '', username: '',
password: '', password: '',
}, },
nameState: null, nameState: null,
urlState: null,
}; };
}, },
methods: { methods: {