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

View File

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

View File

@ -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");
}
};
};
ComunicWeb.user.loginTokens = LoginTokens;

View File

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