Added login tokens storage controller

This commit is contained in:
Pierre 2017-01-28 14:07:18 +01:00
parent 5794179800
commit 29b7a9f4c1
6 changed files with 140 additions and 4 deletions

View File

@ -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);

View File

@ -176,5 +176,17 @@ var ComunicWeb = {
* Get log content in a string
*/
getLogContent: function(){},
}
},
/**
* User functions
*/
user:{
/**
* Login tokens storage controller
*/
loginTokens: {
//TODO: implement
},
},
}

View File

@ -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

View File

@ -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");
}
}
};

View File

@ -1,4 +1,8 @@
<div>
<!--
Error template
@author Pierre HUBERT
--><div>
<h1 style='text-align: center;'><b>{error_code}</b> <small>{error_title}</small></h1>
<p style='text-align:justify;'>

View File

@ -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",
);