From 7c3946f376af8a29e71d5b255f4dbce3de85031f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Feb 2021 15:23:43 +0100 Subject: [PATCH] Improve bad token handling --- assets/js/common/api.js | 21 +++++++++++---------- assets/js/common/shorcuts.js | 2 +- assets/js/common/system.js | 8 +++++--- assets/js/user/userLogin.js | 14 +------------- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/assets/js/common/api.js b/assets/js/common/api.js index 2489bcfc..750d25a6 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -3,7 +3,7 @@ * * @author Pierre HUBERT */ -ComunicWeb.common.api = { +const APIClient = { /** * Make an asynchronous request over the API @@ -151,14 +151,7 @@ ComunicWeb.common.api = { if(apiXHR.readyState == 4){ //Check if response code is 0 - if(apiXHR.status == 0){ - //An error occured - ComunicWeb.common.network.setStatus(false); - } - else{ - //It is a success - ComunicWeb.common.network.setStatus(true); - } + ComunicWeb.common.network.setStatus(apiXHR.status != 0); //Check if response is empty if(apiXHR.responseText.length == ""){ @@ -199,6 +192,12 @@ ComunicWeb.common.api = { if(result.error){ //Log error ComunicWeb.debug.logMessage("Got an error in a XHR request! \n Request URL: "+requestURL+" \n Response : "+apiXHR.responseText); + + if (result.error.code == 412) { + UserLogin.__userLogin = false; + ComunicWeb.user.loginTokens.deleteLoginTokens(); + System.restart(); + } } //We can do the next step @@ -206,4 +205,6 @@ ComunicWeb.common.api = { nextAction(result); } }, -} \ No newline at end of file +} + +ComunicWeb.common.api = APIClient; \ No newline at end of file diff --git a/assets/js/common/shorcuts.js b/assets/js/common/shorcuts.js index e1d63c04..0ac7375a 100644 --- a/assets/js/common/shorcuts.js +++ b/assets/js/common/shorcuts.js @@ -14,7 +14,7 @@ * @return {Promise} */ function api(uri, args, withLogin){ - return ComunicWeb.common.api.exec(uri, args, withLogin); + return APIClient.exec(uri, args, withLogin); } /** diff --git a/assets/js/common/system.js b/assets/js/common/system.js index e4711507..771ac84e 100644 --- a/assets/js/common/system.js +++ b/assets/js/common/system.js @@ -4,7 +4,7 @@ * @author Pierre HUBERT */ -ComunicWeb.common.system = { +const System = { /** * Initializate the application * @@ -94,7 +94,7 @@ ComunicWeb.common.system = { ComunicWeb.common.page.showWaitSplashScreen("Restarting..."); //Reload the page - location.href = document.location; + location.href = document.location.href.split("#")[0]; }, /** @@ -120,4 +120,6 @@ ComunicWeb.common.system = { //Success return true; }, -}; \ No newline at end of file +}; + +ComunicWeb.common.system = System; \ No newline at end of file diff --git a/assets/js/user/userLogin.js b/assets/js/user/userLogin.js index eb3d36b4..3ffd33d1 100644 --- a/assets/js/user/userLogin.js +++ b/assets/js/user/userLogin.js @@ -60,19 +60,7 @@ const UserLogin = { }); } catch(e) { - - //Set user ID to 0 (security purpose) - ComunicWeb.user.userLogin.__userID = 0; - - //If error is 412, make user as logged out - if(result.error.code == 412) { - ComunicWeb.user.userLogin.__userLogin = false; - ComunicWeb.user.loginTokens.deleteLoginTokens(); - - //Restart the application - ComunicWeb.common.system.restart(); - } - + console.error(e); } },