Try to refresh token when failure at startup
This commit is contained in:
parent
cf4d1ec7f2
commit
0c42e0b9b3
1 changed files with 27 additions and 12 deletions
|
@ -51,18 +51,7 @@ var error;
|
|||
alert('There was an error during the authentication');
|
||||
} else {
|
||||
if (access_token) {
|
||||
$.ajax({
|
||||
url: 'https://api.spotify.com/v1/me',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + access_token
|
||||
},
|
||||
success: function (response) {
|
||||
userProfilePlaceholder.innerHTML = userProfileTemplate(response);
|
||||
|
||||
$('#login').hide();
|
||||
$('#loggedin').show();
|
||||
}
|
||||
});
|
||||
getPersonnalInfo(true, userProfilePlaceholder, userProfileTemplate);
|
||||
} else {
|
||||
// render initial screen
|
||||
$('#login').show();
|
||||
|
@ -151,3 +140,29 @@ function refreshToken() {
|
|||
button.removeClass('loading');
|
||||
});
|
||||
}
|
||||
|
||||
function getPersonnalInfo(first, userProfilePlaceholder, userProfileTemplate) {
|
||||
$.ajax({
|
||||
url: 'https://api.spotify.com/v1/me',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + access_token
|
||||
},
|
||||
success: function (response) {
|
||||
userProfilePlaceholder.innerHTML = userProfileTemplate(response);
|
||||
|
||||
$('#login').hide();
|
||||
$('#loggedin').show();
|
||||
},
|
||||
error: function (response) {
|
||||
if (response.status == 401) {
|
||||
if (first) {
|
||||
refreshToken();
|
||||
getPersonnalInfo(false, userProfilePlaceholder, userProfileTemplate);
|
||||
}
|
||||
else {
|
||||
alert("Error getting personnal info");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue