baseui/src/main.js

224 lines
5.9 KiB
JavaScript
Raw Normal View History

2017-08-24 11:16:07 +02:00
import Vue from 'vue';
import Vuex from 'vuex';
import VueRouter from 'vue-router';
2017-08-24 13:23:03 +02:00
import Axios from 'axios';
2017-08-24 20:39:25 +02:00
import JwtDecode from 'jwt-decode';
2017-08-24 13:23:03 +02:00
import App from './app.vue';
2017-08-29 15:51:50 +02:00
import Views from './views/views.js';
2017-08-24 11:16:07 +02:00
Vue.use(VueRouter);
2017-08-24 13:23:03 +02:00
Vue.use(Vuex);
2017-08-24 11:16:07 +02:00
2017-08-29 15:51:50 +02:00
const router = new VueRouter();
2017-08-24 20:39:25 +02:00
2017-08-24 11:16:07 +02:00
const store = new Vuex.Store({
state: {
2017-08-24 15:55:44 +02:00
api: {},
navigation: [],
2017-08-25 12:25:19 +02:00
persist: {
login: {},
authToken: '',
jwt: {},
credentials: {}
}
2017-08-24 11:16:07 +02:00
},
mutations: {
2017-08-24 15:55:44 +02:00
setAPI(state, payload) {
state.api = payload;
},
setNavigation(state, payload) {
2017-08-25 12:25:19 +02:00
for (var i = 0; i < payload.length; i++) {
2017-08-24 15:55:44 +02:00
state.navigation.push(payload[i]); // = payload;
}
},
setCredentials(state, payload) {
2017-08-25 12:25:19 +02:00
state.persist.credentials = {
2017-08-24 15:55:44 +02:00
username: payload.username,
password: payload.password
2017-08-25 12:25:19 +02:00
};
objectToPersist(state.persist, 'persistantStore');
2017-08-24 15:55:44 +02:00
},
setLogin(state, payload) {
2017-08-25 12:25:19 +02:00
state.persist.login = payload.User;
state.persist.authToken = payload.AuthToken;
state.persist.jwt = JwtDecode(payload.AuthToken);
objectToPersist(state.persist, 'persistantStore');
2017-08-24 20:39:25 +02:00
},
2017-08-25 12:25:19 +02:00
clearLogin(state) {
state.persist.login = {};
state.persist.authToken = '';
state.persist.jwt = {};
state.persist.credentials = {};
objectToPersist(state.persist, 'persistantStore');
2017-08-24 15:55:44 +02:00
}
},
actions: {
apiRequest(context, payload) {
2017-08-24 20:39:25 +02:00
let doRequest = () => {
return new Promise((resolve, reject) => {
Axios({
method: payload.method ? payload.method : 'get',
baseURL: context.state.api.baseURL,
url: payload.endpoint,
params: payload.params,
data: payload.data,
headers: {
2017-08-25 12:25:19 +02:00
'X-Auth-Token': context.state.persist.authToken
2017-08-24 20:39:25 +02:00
}
2017-08-24 15:55:44 +02:00
})
2017-08-24 20:39:25 +02:00
.then(response => {
2017-08-25 12:25:19 +02:00
console.log(response);
resolve(response.data);
2017-08-24 20:39:25 +02:00
})
.catch(error => {
2017-08-25 12:25:19 +02:00
console.dir(error);
reject(error);
2017-08-24 20:39:25 +02:00
})
});
};
if (payload.endpoint != 'login') {
// no jwt check for login call
2017-08-25 12:25:19 +02:00
2017-08-24 20:39:25 +02:00
// empty username = not logged in
2017-08-25 12:25:19 +02:00
if (!context.state.persist.credentials.username) {
2017-08-24 20:39:25 +02:00
return new Promise((resolve, reject) => {
// show login page
router.push('/login');
reject(['not logged in']);
});
}
let now = Math.round(Date.now() / 1000);
2017-08-25 12:25:19 +02:00
if (context.state.persist.jwt.exp < (now + 300)) {
2017-08-24 20:39:25 +02:00
// too old jwt, logout
return new Promise((resolve, reject) => {
context.commit('clearLogin');
router.push('/login');
reject(['jwt too old, logout']);
});
}
2017-08-25 12:25:19 +02:00
if ((context.state.persist.jwt.exp - 60) < now) {
2017-08-24 20:39:25 +02:00
// jwt near expire, get new one
console.log("getting new jwt");
2017-08-25 12:25:19 +02:00
return context.dispatch('login', context.state.persist.credentials)
2017-08-24 20:39:25 +02:00
.then(() => {
console.log("LOOOGIIIINNN");
return doRequest();
})
.catch(error => {
throw error;
})
}
}
return doRequest();
2017-08-24 15:55:44 +02:00
},
login(context, payload) {
return new Promise((resolve, reject) => {
context.dispatch('apiRequest', {
method: 'post',
endpoint: 'login',
data: payload
})
.then(data => {
2017-08-24 20:39:25 +02:00
context.commit('setCredentials', payload);
2017-08-24 15:55:44 +02:00
context.commit('setLogin', data);
resolve(data.User);
})
.catch(error => {
2017-08-25 12:25:19 +02:00
if (error.response && error.response.data && error.response.data.error) {
reject(error.response.data.error);
} else {
reject([]);
}
2017-08-24 15:55:44 +02:00
});
});
}
2017-08-24 11:16:07 +02:00
}
2017-08-24 15:55:44 +02:00
});
2017-08-25 12:25:19 +02:00
function objectToPersist(obj, key) {
try {
const serialized = JSON.stringify(obj);
localStorage.setItem(key ? key : 'persistant', serialized);
} catch (error) {
console.error(error);
}
};
function persistToObject(key) {
try {
const serialized = localStorage.getItem(key ? key : 'persistant');
if (serialized === null) {
return undefined;
}
return JSON.parse(serialized);
} catch (error) {
console.log(error);
return undefined;
}
}
// get store persist part from localStorage
let persist = persistToObject('persistantStore');
if (persist) {
store.state.persist = persist;
}
2017-08-24 15:55:44 +02:00
// load init
Axios.get('conf/init.json')
2017-08-25 12:25:19 +02:00
.then(results => {
// set navigation
if (!Array.isArray(results.data.navigation)) {
alert('invalid data in init.json');
return;
}
2017-08-29 21:38:33 +02:00
const navigation = results.data.navigation.map(({name, to, icon, show}) => {return {name, to, icon, show}});
2017-08-29 16:34:24 +02:00
2017-08-29 15:51:50 +02:00
store.commit("setNavigation", navigation);
2017-08-25 12:25:19 +02:00
// set api config in store
store.commit("setAPI", results.data.api);
2017-08-29 15:51:50 +02:00
// add routes
let routes = [];
let rIdx = 0;
2017-08-29 16:34:24 +02:00
results.data.navigation.forEach(({name, to, content, data}) => routes.push({
name: name,
2017-08-29 15:51:50 +02:00
path: to,
2017-08-29 16:34:24 +02:00
meta: {
title: name
},
component: Vue.component(name + rIdx++, {
template: '<div id="' + name + rIdx + '">' + content + '</div>',
2017-08-29 15:51:50 +02:00
components: Views,
data: function(data) {
2017-08-29 15:51:50 +02:00
if (typeof data != 'object') {
return () => { return {}; };
2017-08-29 15:51:50 +02:00
}
return () => { return data; };
}(data)
2017-08-29 15:51:50 +02:00
})
}));
router.addRoutes(routes);
2017-08-29 16:34:24 +02:00
router.beforeEach((to, from, next) => {
console.log(to);
document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title;
next();
});
2017-08-29 15:51:50 +02:00
2017-08-25 12:25:19 +02:00
// load app, when init finishs
new Vue({
el: '#vue-app',
2017-08-29 15:51:50 +02:00
render: h => h(App),
2017-08-25 12:25:19 +02:00
router,
store
});
})
.catch(error => {
alert('error loading: ' + error.message);
});