koel/resources/assets/js/main.js
2016-01-26 14:24:23 +08:00

46 lines
1.1 KiB
JavaScript

import ls from './services/ls';
window.Vue = require('vue');
var app = new Vue(require('./app.vue'));
Vue.config.debug = false;
Vue.use(require('vue-resource'));
Vue.http.options.root = '/api';
Vue.http.interceptors.push({
request(r) {
var token = ls.get('jwt-token');
if (token) {
Vue.http.headers.common.Authorization = `Bearer ${token}`;
}
return r;
},
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();
}
}
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;
},
});
/**
* Thor! Odin's son, protector of Mankind
* Ride to meet your fate, your destiny awaits
* Thor! Hlödyn's son, protector of Mankind
* Ride to meet your fate, Ragnarök awaits
*/
app.$mount('body');