Last active
May 9, 2016 19:05
-
-
Save nivv/f41f2bb2486e8057cc0f5c931a67d7bc to your computer and use it in GitHub Desktop.
Retry request vue-resource
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 characters
fetchArticles: function() { | |
this.$http.get({url: '/api/v2/articles'}).then( (response) => { | |
// success callback | |
this.$set('articles', response.data.data); | |
this.$set('appReady', true); | |
}, (response) => { | |
if (response.status == 401) { | |
//Seems like token is expired, refresh and retry | |
console.log('Should see if we can refresh the token!') | |
auth.handle(response); | |
//auth.refreshToken(); | |
//auth.refreshToken(this, store.state.failedRequest); | |
} | |
// error callback | |
}); | |
}, |
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 characters
export default { | |
handle: function(response) { | |
var retry = function() { | |
var tempToken = localStorage.getItem('id_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) | |
var tempToken = localStorage.getItem('id_token'); | |
return Vue.http.post('/api/v2/refresh-token', tempToken) | |
.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); | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment