globalConf

This commit is contained in:
Sebastian Frank 2017-09-25 12:53:06 +02:00
parent b39117e074
commit 92e756dbea

View File

@ -21,6 +21,14 @@ Vue.component('textarea-input', TextareaInput);
Vue.component('my-table', MyTable);
Vue.component('scroll-table', ScrollTable);
const globalConf = {
loginEndpoint: 'login',
loginRoute: '/login',
authHeader: 'X-Auth-Token',
initUrl: 'conf/init.json',
el: '#my-app'
}
const Router = new VueRouter();
const Store = new Vuex.Store({
@ -62,15 +70,15 @@ const Store = new Vuex.Store({
apiRequest(context, payload) {
let doRequest = () => {
return new Promise((resolve, reject) => {
const authH = {};
authH[globalConf.authHeader] = context.state.persist.authToken;
Axios({
method: payload.method ? payload.method : 'get',
baseURL: context.state.ui.api.baseURL,
url: payload.endpoint,
params: payload.params,
data: payload.data,
headers: {
'X-Auth-Token': context.state.persist.authToken
}
headers: authH
})
.then(response => {
console.log(response);
@ -83,14 +91,14 @@ const Store = new Vuex.Store({
});
};
if (payload.endpoint != 'login') {
if (payload.endpoint != globalConf.loginEndpoint) {
// no jwt check for login call
// empty username = not logged in
if (!context.state.persist.credentials.username) {
return new Promise((resolve, reject) => {
// show login page
Router.push('/login');
Router.push(globalConf.loginRoute);
reject(['not logged in']);
});
}
@ -100,7 +108,7 @@ const Store = new Vuex.Store({
// too old jwt, logout
return new Promise((resolve, reject) => {
context.commit('clearLogin');
Router.push('/login');
Router.push(globalConf.loginRoute);
reject(['jwt too old, logout']);
});
}
@ -109,7 +117,7 @@ const Store = new Vuex.Store({
// jwt near expire, get new one
console.log("getting new jwt");
return context.dispatch('login', context.state.persist.credentials)
return context.dispatch(globalConf.loginEndpoint, context.state.persist.credentials)
.then(() => {
console.log("LOOOGIIIINNN");
return doRequest();
@ -126,7 +134,7 @@ const Store = new Vuex.Store({
return new Promise((resolve, reject) => {
context.dispatch('apiRequest', {
method: 'post',
endpoint: 'login',
endpoint: globalConf.loginEndpoint,
data: payload
})
.then(data => {
@ -180,7 +188,8 @@ export default {
Store,
Axios,
InitApp(config) { // config: {initUrl, views, el}
Axios.get(config.initUrl)
Object.assign(globalConf, config);
Axios.get(globalConf.initUrl)
.then(results => {
// set navigation
if (!Array.isArray(results.data.routes)) {
@ -202,7 +211,7 @@ export default {
},
component: {
template: '<div id="' + name + rIdx + '">' + content + '</div>',
components: config.views,
components: globalConf.views,
data: function (data) {
if (typeof data != 'object') {
return () => { return {}; };
@ -219,7 +228,7 @@ export default {
// load app, when init finishs
new Vue({
el: config.el,
el: globalConf.el,
render: h => h(App),
router: Router,
store: Store