Improve tokens management

This commit is contained in:
Pierre HUBERT 2021-02-13 15:43:58 +01:00
parent 568239b6f5
commit 72e93fbc6c
4 changed files with 34 additions and 95 deletions

View File

@ -46,15 +46,7 @@ const APIClient = {
//Add login tokens to params if required //Add login tokens to params if required
if(requireLoginTokens){ if(requireLoginTokens){
//Get login tokens params.token = LoginTokens.getLoginToken();
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
if(tokens){
//Add tokens
params.userToken1 = tokens.token1;
params.userToken2 = tokens.token2;
}
} }
//Enable incognito mode if required //Enable incognito mode if required
@ -103,21 +95,12 @@ const APIClient = {
//Prepare the request URL //Prepare the request URL
var requestURL = ComunicWeb.__config.apiURL + apiURI; var requestURL = ComunicWeb.__config.apiURL + apiURI;
//Add API service tokens // Add API client name
data.append('serviceName', ComunicWeb.__config.apiServiceName); data.append('client', ComunicWeb.__config.apiClientName);
data.append('serviceToken', ComunicWeb.__config.apiServiceToken);
//Add login tokens to params if required //Add login tokens to params if required
if(requireLoginTokens){ if(requireLoginTokens){
//Get login tokens data.append("token", LoginTokens.getLoginToken())
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
if(tokens){
//Add tokens
data.append('userToken1', tokens.token1);
data.append('userToken2', tokens.token2);
}
} }
//Enable incognito mode if required //Enable incognito mode if required

View File

@ -340,30 +340,6 @@ var ComunicWeb = {
* User functions * User functions
*/ */
user:{ 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 * Get user infos

View File

@ -5,30 +5,27 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
ComunicWeb.user.loginTokens = { const LoginTokens = {
/** /**
* Set User tokens * Set User tokens
* *
* @param {Object} tokens The tokens object * @param {String} token The token
* @param {Type} storageType The token destination (local or session) * @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 //First, we check if there is any login token available
this.deleteLoginTokens(); this.deleteLoginTokens();
//We encode login tokens
var tokensArray = JSON.stringify(tokens);
//We store login tokens //We store login tokens
//If localStorage is required //If localStorage is required
if(storageType == "local"){ if(storageType == "local")
localStorage.setItem("loginTokens", tokensArray); localStorage.setItem("loginToken", token);
}
else { else
//Session storage //Session storage
sessionStorage.setItem("loginTokens", tokensArray); sessionStorage.setItem("loginToken", token);
}
//Everything is OK //Everything is OK
return true; return true;
@ -42,49 +39,33 @@ ComunicWeb.user.loginTokens = {
*/ */
checkLoginTokens: function(){ checkLoginTokens: function(){
//First, check in local storage //First, check in local storage
if(localStorage.getItem("loginTokens") != null){ if(localStorage.getItem("loginToken") != null)
//It is OK
return true; return true;
}
//Check if we have to remove any thing in session storage //Check if we have to remove any thing in session storage
if(sessionStorage.getItem("loginTokens") != null){ if(sessionStorage.getItem("loginToken") != null)
//It is OK
return true; return true;
}
//Else there isn't login token available
return false; 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 //First, check in local storage
if(localStorage.getItem("loginTokens") !== null){ if(localStorage.getItem("loginToken") !== null)
//Return localStorage login tokens return localStorage.getItem("loginToken");
var loginTokenString = localStorage.getItem("loginTokens");
}
//Then, check in session storage //Then, check in session storage
if(sessionStorage.getItem("loginTokens") !== null){ if(sessionStorage.getItem("loginToken") !== null){
//Return session storage login token return sessionStorage.getItem("loginToken");
var loginTokenString = sessionStorage.getItem("loginTokens");
} }
//Check if we didn't get any login token
if(!loginTokenString){
return false; return false;
}
//Decode the login token
var loginTokens = JSON.parse(loginTokenString);
//Returns the result
return loginTokens;
}, },
/** /**
@ -92,15 +73,14 @@ ComunicWeb.user.loginTokens = {
*/ */
deleteLoginTokens: function(){ deleteLoginTokens: function(){
//Check if we have to remove any thing in local storage //Check if we have to remove any thing in local storage
if(localStorage.getItem("loginTokens") != "null"){ if(localStorage.getItem("loginToken") != "null")
//Remove the key localStorage.removeItem("loginToken");
localStorage.removeItem("loginTokens");
}
//Check if we have to remove any thing in session storage //Check if we have to remove any thing in session storage
if(sessionStorage.getItem("loginTokens") != "null"){ if(sessionStorage.getItem("loginToken") != "null")
//Remove the key sessionStorage.removeItem("loginToken");
sessionStorage.removeItem("loginTokens");
}
} }
}; };
ComunicWeb.user.loginTokens = LoginTokens;

View File

@ -115,7 +115,7 @@ const UserLogin = {
else { else {
storageType = "session"; storageType = "session";
} }
ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType); LoginTokens.setUserToken(result.token, storageType);
// Save email address // Save email address
ComunicWeb.components.mailCaching.set(usermail); ComunicWeb.components.mailCaching.set(usermail);
@ -153,7 +153,7 @@ const UserLogin = {
//Destroy login tokens //Destroy login tokens
ComunicWeb.user.loginTokens.deleteLoginTokens(); LoginTokens.deleteLoginTokens();
//Specify user is logged out //Specify user is logged out
this.__userID = 0; this.__userID = 0;