Created
January 7, 2016 22:41
-
-
Save kirkbushell/6b60a44c0949d08feb46 to your computer and use it in GitHub Desktop.
Handling JWT, Vue JS and token refreshes
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
import Unauthorised from './Unauthorised' | |
export default function() { | |
return { | |
response: function(response) { | |
switch (response.status) { | |
case 401: return Unauthorised.handle(); | |
} | |
return response; | |
} | |
} | |
}; |
@kirkbushell, no worries! Feels a bit silly to be able to do the refresh of the token, and then retry the request but then not being able to use the data in the response.
I solved it using vuex and a watcher on the component.
/**
* 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();
console.log('old token, retrying')
return Vue.http[method](request.url, request.params).then( (response) => {
response['done'] = true;
store.dispatch('SET_FAILED_REQUEST_DATA', response);
}, (response) => {
this.redirect();
});
},
In my component
watch: {
'failedRequestData': function(val) {
if(val.done === true) {
this.$set('articles', this.failedRequestData.data.data);
this.$set('appReady', true);
store.dispatch('SET_FAILED_REQUEST_DATA', {done: false});
}
}
},
What if i have more no.of components? Let's say i have 100 components, in this case, we can't write watchers in all the components right? So what would be the best solution for it, to pass the data to the previous call?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nivv tbh I'm not really sure. This a bit old now, but I do remember having a number of issues (about 2 days worth of time) to get it working in the end. Was a stack of trial and error. Sorry I can't be more help.