Improve bad token handling

This commit is contained in:
Pierre HUBERT 2021-02-13 15:23:43 +01:00
parent 9dfe0e39eb
commit 7c3946f376
4 changed files with 18 additions and 27 deletions

View File

@ -3,7 +3,7 @@
* *
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
ComunicWeb.common.api = { const APIClient = {
/** /**
* Make an asynchronous request over the API * Make an asynchronous request over the API
@ -151,14 +151,7 @@ ComunicWeb.common.api = {
if(apiXHR.readyState == 4){ if(apiXHR.readyState == 4){
//Check if response code is 0 //Check if response code is 0
if(apiXHR.status == 0){ ComunicWeb.common.network.setStatus(apiXHR.status != 0);
//An error occured
ComunicWeb.common.network.setStatus(false);
}
else{
//It is a success
ComunicWeb.common.network.setStatus(true);
}
//Check if response is empty //Check if response is empty
if(apiXHR.responseText.length == ""){ if(apiXHR.responseText.length == ""){
@ -199,6 +192,12 @@ ComunicWeb.common.api = {
if(result.error){ if(result.error){
//Log error //Log error
ComunicWeb.debug.logMessage("Got an error in a XHR request! \n Request URL: "+requestURL+" \n Response : "+apiXHR.responseText); 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 //We can do the next step
@ -206,4 +205,6 @@ ComunicWeb.common.api = {
nextAction(result); nextAction(result);
} }
}, },
} }
ComunicWeb.common.api = APIClient;

View File

@ -14,7 +14,7 @@
* @return {Promise} * @return {Promise}
*/ */
function api(uri, args, withLogin){ function api(uri, args, withLogin){
return ComunicWeb.common.api.exec(uri, args, withLogin); return APIClient.exec(uri, args, withLogin);
} }
/** /**

View File

@ -4,7 +4,7 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
ComunicWeb.common.system = { const System = {
/** /**
* Initializate the application * Initializate the application
* *
@ -94,7 +94,7 @@ ComunicWeb.common.system = {
ComunicWeb.common.page.showWaitSplashScreen("Restarting..."); ComunicWeb.common.page.showWaitSplashScreen("Restarting...");
//Reload the page //Reload the page
location.href = document.location; location.href = document.location.href.split("#")[0];
}, },
/** /**
@ -120,4 +120,6 @@ ComunicWeb.common.system = {
//Success //Success
return true; return true;
}, },
}; };
ComunicWeb.common.system = System;

View File

@ -60,19 +60,7 @@ const UserLogin = {
}); });
} catch(e) { } catch(e) {
console.error(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();
}
} }
}, },