From 0c42e0b9b3fb6e4428b6d9ad692629eb5fdb192a Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 17 Apr 2016 17:52:10 +0200 Subject: [PATCH] Try to refresh token when failure at startup --- public/script.js | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/public/script.js b/public/script.js index 890ca95..3072b40 100644 --- a/public/script.js +++ b/public/script.js @@ -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"); + } + } + } + }); +}