From 72e93fbc6c98c9631b8513f543902fb27390e60a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Feb 2021 15:43:58 +0100 Subject: [PATCH] Improve tokens management --- assets/js/common/api.js | 25 ++-------- assets/js/common/functionsSchema.js | 24 --------- assets/js/user/loginTokens.js | 76 +++++++++++------------------ assets/js/user/userLogin.js | 4 +- 4 files changed, 34 insertions(+), 95 deletions(-) diff --git a/assets/js/common/api.js b/assets/js/common/api.js index e87278f6..f6967c06 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -46,15 +46,7 @@ const APIClient = { //Add login tokens to params if required if(requireLoginTokens){ - //Get login tokens - tokens = ComunicWeb.user.loginTokens.getLoginTokens(); - - if(tokens){ - //Add tokens - params.userToken1 = tokens.token1; - params.userToken2 = tokens.token2; - } - + params.token = LoginTokens.getLoginToken(); } //Enable incognito mode if required @@ -103,21 +95,12 @@ const APIClient = { //Prepare the request URL var requestURL = ComunicWeb.__config.apiURL + apiURI; - //Add API service tokens - data.append('serviceName', ComunicWeb.__config.apiServiceName); - data.append('serviceToken', ComunicWeb.__config.apiServiceToken); + // Add API client name + data.append('client', ComunicWeb.__config.apiClientName); //Add login tokens to params if required if(requireLoginTokens){ - //Get login tokens - tokens = ComunicWeb.user.loginTokens.getLoginTokens(); - - if(tokens){ - //Add tokens - data.append('userToken1', tokens.token1); - data.append('userToken2', tokens.token2); - } - + data.append("token", LoginTokens.getLoginToken()) } //Enable incognito mode if required diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index a0f98e4c..b57739ab 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -340,30 +340,6 @@ var ComunicWeb = { * User functions */ user:{ - /** - * Login tokens storage controller - */ - loginTokens: { - /** - * Set User tokens - */ - setUserTokens: function(tokens, storageType){}, - - /** - * Check if there is any login tokens available - */ - checkLoginTokens: function(){}, - - /** - * Get login tokens - */ - getLoginTokens: function(){}, - - /** - * Perform user logout - */ - deleteLoginTokens: function(){}, - }, /** * Get user infos diff --git a/assets/js/user/loginTokens.js b/assets/js/user/loginTokens.js index 284c8ba7..364b14ac 100644 --- a/assets/js/user/loginTokens.js +++ b/assets/js/user/loginTokens.js @@ -5,30 +5,27 @@ * @author Pierre HUBERT */ -ComunicWeb.user.loginTokens = { +const LoginTokens = { /** * Set User tokens * - * @param {Object} tokens The tokens object + * @param {String} token The token * @param {Type} storageType The token destination (local or session) */ - setUserTokens: function(tokens, storageType){ + setUserToken: function(token, storageType){ //First, we check if there is any login token available this.deleteLoginTokens(); - //We encode login tokens - var tokensArray = JSON.stringify(tokens); - //We store login tokens //If localStorage is required - if(storageType == "local"){ - localStorage.setItem("loginTokens", tokensArray); - } - else { + if(storageType == "local") + localStorage.setItem("loginToken", token); + + else //Session storage - sessionStorage.setItem("loginTokens", tokensArray); - } + sessionStorage.setItem("loginToken", token); + //Everything is OK return true; @@ -42,49 +39,33 @@ ComunicWeb.user.loginTokens = { */ checkLoginTokens: function(){ //First, check in local storage - if(localStorage.getItem("loginTokens") != null){ - //It is OK + if(localStorage.getItem("loginToken") != null) return true; - } //Check if we have to remove any thing in session storage - if(sessionStorage.getItem("loginTokens") != null){ - //It is OK + if(sessionStorage.getItem("loginToken") != null) return true; - } - //Else there isn't login token available return false; }, /** - * Get login tokens + * Get login token * - * @return {Object} Login tokens, if they exists (false in failure) + * @return {Object} Login token, if they exists (false in failure) */ - getLoginTokens: function(){ + getLoginToken: function(){ //First, check in local storage - if(localStorage.getItem("loginTokens") !== null){ - //Return localStorage login tokens - var loginTokenString = localStorage.getItem("loginTokens"); - } + if(localStorage.getItem("loginToken") !== null) + return localStorage.getItem("loginToken"); + //Then, check in session storage - if(sessionStorage.getItem("loginTokens") !== null){ - //Return session storage login token - var loginTokenString = sessionStorage.getItem("loginTokens"); + if(sessionStorage.getItem("loginToken") !== null){ + return sessionStorage.getItem("loginToken"); } - //Check if we didn't get any login token - if(!loginTokenString){ - return false; - } - - //Decode the login token - var loginTokens = JSON.parse(loginTokenString); - - //Returns the result - return loginTokens; + return false; }, /** @@ -92,15 +73,14 @@ ComunicWeb.user.loginTokens = { */ deleteLoginTokens: function(){ //Check if we have to remove any thing in local storage - if(localStorage.getItem("loginTokens") != "null"){ - //Remove the key - localStorage.removeItem("loginTokens"); - } + if(localStorage.getItem("loginToken") != "null") + localStorage.removeItem("loginToken"); + //Check if we have to remove any thing in session storage - if(sessionStorage.getItem("loginTokens") != "null"){ - //Remove the key - sessionStorage.removeItem("loginTokens"); - } + if(sessionStorage.getItem("loginToken") != "null") + sessionStorage.removeItem("loginToken"); } -}; \ No newline at end of file +}; + +ComunicWeb.user.loginTokens = LoginTokens; \ No newline at end of file diff --git a/assets/js/user/userLogin.js b/assets/js/user/userLogin.js index e2a8499f..4caaaaed 100644 --- a/assets/js/user/userLogin.js +++ b/assets/js/user/userLogin.js @@ -115,7 +115,7 @@ const UserLogin = { else { storageType = "session"; } - ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType); + LoginTokens.setUserToken(result.token, storageType); // Save email address ComunicWeb.components.mailCaching.set(usermail); @@ -153,7 +153,7 @@ const UserLogin = { //Destroy login tokens - ComunicWeb.user.loginTokens.deleteLoginTokens(); + LoginTokens.deleteLoginTokens(); //Specify user is logged out this.__userID = 0;