From 29b7a9f4c10574f3b9026cc5de6e2e9a9e17fdb6 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 28 Jan 2017 14:07:18 +0100 Subject: [PATCH] Added login tokens storage controller --- assets/js/common/errors.js | 4 +- assets/js/common/functionsSchema.js | 14 ++- assets/js/common/page.js | 12 ++- assets/js/user/loginTokens.js | 105 +++++++++++++++++++++++ assets/templates/common/errors/error.tpl | 6 +- corePage/config/dev.config.php | 3 + 6 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 assets/js/user/loginTokens.js diff --git a/assets/js/common/errors.js b/assets/js/common/errors.js index d9532fda..78e6fdc8 100644 --- a/assets/js/common/errors.js +++ b/assets/js/common/errors.js @@ -83,7 +83,9 @@ ComunicWeb.common.error.pageNotFound = function(additionnalData, targetElement){ //Show template element var templateURI = "common/errors/error.tpl"; var dataTemplate = { - + error_code: "404", + error_title: "Page not found", + error_message: "The requested page ("+location.href+") was not found on this website. Please check your request..." }; ComunicWeb.common.page.getAndShowTemplate(targetElement, dataTemplate, templateURI, (function(){}), true); diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 1e6088e5..7720b802 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -176,5 +176,17 @@ var ComunicWeb = { * Get log content in a string */ getLogContent: function(){}, - } + }, + + /** + * User functions + */ + user:{ + /** + * Login tokens storage controller + */ + loginTokens: { + //TODO: implement + }, + }, } \ No newline at end of file diff --git a/assets/js/common/page.js b/assets/js/common/page.js index ca99e11e..aab04ed9 100644 --- a/assets/js/common/page.js +++ b/assets/js/common/page.js @@ -143,7 +143,17 @@ ComunicWeb.common.page = { //Define how to apply the template var afterDownloadTemplateContent = function(templateContent){ - targetElem.innerHTML = (templateContent); + + //Apply data templates + for(elemName in dataTemplate){ + //We change the template content while it still exists + while(templateContent.indexOf("{"+elemName+"}") != -1){ + templateContent = templateContent.replace("{"+elemName+"}", dataTemplate[elemName]); + } + } + + //Apply template source + targetElem.innerHTML = templateContent; } //Perform request diff --git a/assets/js/user/loginTokens.js b/assets/js/user/loginTokens.js new file mode 100644 index 00000000..2ff4b02d --- /dev/null +++ b/assets/js/user/loginTokens.js @@ -0,0 +1,105 @@ +/** + * User functions + * - Login tokens + * + * @author Pierre HUBERT + */ + +ComunicWeb.user.loginTokens = { + /** + * Set User tokens + * + * @param {Object} tokens The token object + * @param {Type} storageType The token destination (local or session) + */ + setUserTokens: function(tokens, 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 { + //Session storage + sessionStorage.setItem("loginTokens", tokensArray); + } + + //Everything is OK + return true; + + }, + + /** + * Check if there is any login tokens available + * + * @return {Boolean} True or false, depending of the result + */ + checkLoginTokens: function(){ + //First, check in local storage + if(localStorage.getItem("loginTokens") != "null"){ + //It is OK + return true; + } + + //Check if we have to remove any thing in session storage + if(sessionStorage.getItem("loginTokens") != "null"){ + //It is OK + return true; + } + + //Else there isn't login token available + return false; + }, + + /** + * Get login tokens + * + * @return {Object} Login tokens, if they exists (false in failure) + */ + getLoginTokens: function(){ + //First, check in local storage + if(localStorage.getItem("loginTokens") != "null"){ + //Return localStorage login tokens + var loginTokenString = localStorage.getItem("loginTokens") + } + + //Then, check in session storage + if(sessionStorage.getItem("loginTokens") != "null"){ + //Return session storage login token + var loginTokenString = sessionStorage.getItem("loginTokens") + } + + //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; + }, + + /** + * Perform user logout (delete tokens) + */ + deleteLoginTokens: function(){ + //Check if we have to remove any thing in local storage + if(localStorage.getItem("loginTokens") != "null"){ + //Remove the key + localStorage.removeItem("loginTokens"); + } + + //Check if we have to remove any thing in session storage + if(sessionStorage.getItem("loginTokens") != "null"){ + //Remove the key + sessionStorage.removeItem("loginTokens"); + } + } +}; \ No newline at end of file diff --git a/assets/templates/common/errors/error.tpl b/assets/templates/common/errors/error.tpl index 8aafdd1b..34cd6adb 100644 --- a/assets/templates/common/errors/error.tpl +++ b/assets/templates/common/errors/error.tpl @@ -1,4 +1,8 @@ -
+

{error_code} {error_title}

diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index f5fe946f..517ae60f 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -43,6 +43,9 @@ $config['JSfiles'] = array( "%PATH_ASSETS%js/langs/en.inc.js", "%PATH_ASSETS%js/common/page.js", + //User scripts + "%PATH_ASSETS%js/user/loginTokens.js", + //Init script "%PATH_ASSETS%js/init.js", );