import Vue from "vue" import JWT from "../../components/authentication/JWT" export default { handle: function(response) { var retry = function() { JWT.setToken(response.data.token); return this.retry(response.request).then(function() { return response; }); }.bind(this); // Refresh JWT, then retry previous request if successful, and catch any errors (auth fail of some kind) return Vue.http.get('/auth/refresh') .then(retry) .catch(this.redirect); }, /** * Retries the request initially made by the app before an auth error was thrown. * * @param request * @return Promise */ retry: function(request) { var method = request.method.toLowerCase(); return Vue.http[method](request.url, request.params); }, redirect: function() { window.location.href = '/auth'; } };