koel/resources/assets/js/main.js
2016-07-08 12:46:15 +08:00

55 lines
1.1 KiB
JavaScript

import Vue from 'vue';
import NProgress from 'nprogress';
import { ls } from './services';
import { event } from './utils';
import App from './app.vue'
Vue.config.debug = false;
Vue.use(require('vue-resource'));
Vue.http.options.root = '/api';
Vue.http.interceptors.push({
request(r) {
const token = ls.get('jwt-token');
if (token) {
Vue.http.headers.common.Authorization = `Bearer ${token}`;
}
return r;
},
response(r) {
NProgress.done();
if (r.status === 400 || r.status === 401) {
if (!(r.request.method === 'POST' && r.request.url === 'me')) {
// This is not a failed login. Log out then.
event.emit('logout');
}
}
if (r.headers && r.headers.Authorization) {
ls.set('jwt-token', r.headers.Authorization);
}
if (r.data && r.data.token && r.data.token.length > 10) {
ls.set('jwt-token', r.data.token);
}
return r;
},
});
/**
* For Ancelot, the ancient cross of war
* for the holy town of Gods
* Gloria, gloria perpetua
* in this dawn of victory
*/
new Vue({
el: '#app',
render: h => h(App),
created() { event.init() },
});