Created
March 7, 2016 20:57
-
-
Save khle/60c6f5ba1671ffe289b8 to your computer and use it in GitHub Desktop.
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
| 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