koel/resources/assets/js/main.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2015-12-29 01:35:22 +00:00
import ls from './services/ls';
2015-12-13 04:42:28 +00:00
window.Vue = require('vue');
2015-12-30 04:14:47 +00:00
var app = new Vue(require('./app.vue'));
2015-12-29 01:35:22 +00:00
Vue.config.debug = false;
2015-12-13 04:42:28 +00:00
Vue.use(require('vue-resource'));
Vue.http.options.root = '/api';
2015-12-29 01:35:22 +00:00
Vue.http.interceptors.push({
2016-01-01 06:16:17 +00:00
request(r) {
2015-12-29 01:35:22 +00:00
var token = ls.get('jwt-token');
if (token) {
2015-12-30 04:14:47 +00:00
Vue.http.headers.common.Authorization = `Bearer ${token}`;
2015-12-29 01:35:22 +00:00
}
2016-01-01 06:16:17 +00:00
return r;
2015-12-29 01:35:22 +00:00
},
2016-01-01 06:16:17 +00:00
response(r) {
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.
app.logout();
}
2015-12-29 01:35:22 +00:00
}
2016-01-01 06:16:17 +00:00
if (r.headers && r.headers.Authorization) {
ls.set('jwt-token', r.headers.Authorization);
2015-12-29 01:35:22 +00:00
}
2016-01-01 06:16:17 +00:00
if (r.data && r.data.token && r.data.token.length > 10) {
ls.set('jwt-token', r.data.token);
2015-12-29 01:35:22 +00:00
}
2016-01-01 06:16:17 +00:00
return r;
2015-12-29 01:35:22 +00:00
},
});
2015-12-13 04:42:28 +00:00
// Exit light,
// Enter night,
// Take my hand,
// We're off to never never land.
2015-12-30 04:14:47 +00:00
app.$mount('body');