Created
January 7, 2016 22:41
Revisions
-
kirkbushell created this gist
Jan 7, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ import Unauthorised from './Unauthorised' export default function() { return { response: function(response) { switch (response.status) { case 401: return Unauthorised.handle(); } return response; } } }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ 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'; } };