Skip to content

Instantly share code, notes, and snippets.

@khle
Created March 7, 2016 20:57
Show Gist options
  • Select an option

  • Save khle/60c6f5ba1671ffe289b8 to your computer and use it in GitHub Desktop.

Select an option

Save khle/60c6f5ba1671ffe289b8 to your computer and use it in GitHub Desktop.
getAuthorizationHeader() {
if (cacheToken.access_token && cacheToken.expires_on > moment(new Date().getTime()).unix()) {
return $q.when({'Authorization': 'Bearer ' + cacheToken.access_token});
} else {
cacheToken.access_token = storage.getItem('access_token');
cacheToken.refresh_token = storage.getItem('refresh_token');
cacheToken.expires_on = storage.getItem('expires_on');
if (cacheToken.access_token && cacheToken.expires_on > moment(new Date().getTime()).unix()) {
return $q.when({'Authorization': 'Bearer ' + cacheToken.access_token});
} else {
return $http.post('/refreshToken', {'token': cacheToken.refresh_token}).then(
response => {
var token = response.data;
cacheToken.access_token = token.access_token;
storage.setItem('access_token', cacheToken.access_token);
cacheToken.refresh_token = token.refresh_token;
storage.setItem('refresh_token', cacheToken.refresh_token);
cacheToken.expires_on = token.expires_on;
storage.setItem('expires_on', cacheToken.expires_on);
return {'Authorization': 'Bearer ' + cacheToken.access_token};
},
err => {
console.log('Error refreshing token ' + err)
}
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment