Added login system

This commit is contained in:
Pierre 2017-01-29 15:49:21 +01:00
parent 48323ed9af
commit 82b009bdcc
6 changed files with 232 additions and 13 deletions

View File

@ -9,12 +9,23 @@
* *
* @param {String} apiURI The URI to call in the API * @param {String} apiURI The URI to call in the API
* @param {Object} params The params to include in request * @param {Object} params The params to include in request
* @param {Boolean} requireLoginTokens Specify if login tokens are required or not
* @param {Function} nextAction What to do next * @param {Function} nextAction What to do next
*/ */
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginTokens, nextAction){
//Prepare the request URL //Prepare the request URL
var requestURL = ComunicWeb.__config.apiURL + apiURI; var requestURL = ComunicWeb.__config.apiURL + apiURI;
//Add login tokens to params if required
if(requireLoginTokens){
//Get login tokens
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
//Add tokens
params.token1 = tokens.token1;
params.token2 = tokens.token2;
}
//Prepare data to send in request //Prepare data to send in request
var count = 0; var count = 0;
var datas = ""; var datas = "";
@ -27,7 +38,7 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]); datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
count++; //Increment counter count++; //Increment counter
} }
//Create request //Create request
var apiXHR = new XMLHttpRequest(); var apiXHR = new XMLHttpRequest();
@ -38,10 +49,16 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
//We continue only if request is terminated //We continue only if request is terminated
if(apiXHR.readyState == 4){ if(apiXHR.readyState == 4){
//Prepare result //Prepare result
var result = {}; var result = JSON.parse(apiXHR.responseText);
//We check if we got any error
if(result.error){
//Log error
ComunicWeb.debug.logMessage("Got an error in a XHR request! " + result.toString());
}
//We can do the next step //We can do the next step
nextAction(apiXHR.responseText); nextAction(result);
} }
} }

View File

@ -21,12 +21,13 @@ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCo
"errorCode": errorCode, "errorCode": errorCode,
"errorData": errorData.toSource(), "errorData": errorData.toSource(),
} }
var requireLoginToken = false;
//Not any next action for now //Not any next action for now
nextAction = function(){}; nextAction = function(){};
//Send API request //Send API request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, nextAction); ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginToken, nextAction);
} }
/** /**

View File

@ -21,7 +21,7 @@ var ComunicWeb = {
/** /**
* Make an API request * Make an API request
*/ */
makeAPIrequest: function(apiURI, params, nextAction){}, makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){},
}, },
/** /**
@ -211,7 +211,47 @@ var ComunicWeb = {
* Manage user login * Manage user login
*/ */
userLogin: { userLogin: {
/**
* @var {Boolean} Store user login state (true by default)
*/
__userLogin: true,
/**
* @var {Integer} Store the current user ID
*/
__userID: 0,
/**
* Tell if user is logged in or not
*/
getUserLoginState: function(){},
/**
* Get user ID (if logged in)
*/
getUserID: function(){},
/**
* Try to get and store current user ID
*/
getCurrentUserId: function(afterGetCurrentUserID){},
/**
* Refresh the user login state
*/
refreshLoginState: function(){},
/**
* Try to login user
*/
loginUser: function(usermail, userpassword, permanentLogin, afterLogin){},
/**
* Get user infos
*/
userInfos: {
//TODO: implement //TODO: implement
}, }
}, },
} }

View File

@ -10,7 +10,7 @@ ComunicWeb.user.loginTokens = {
/** /**
* Set User tokens * Set User tokens
* *
* @param {Object} tokens The token object * @param {Object} tokens The tokens object
* @param {Type} storageType The token destination (local or session) * @param {Type} storageType The token destination (local or session)
*/ */
setUserTokens: function(tokens, storageType){ setUserTokens: function(tokens, storageType){
@ -64,15 +64,15 @@ ComunicWeb.user.loginTokens = {
*/ */
getLoginTokens: function(){ getLoginTokens: function(){
//First, check in local storage //First, check in local storage
if(localStorage.getItem("loginTokens") != "null"){ if(localStorage.getItem("loginTokens") !== null){
//Return localStorage login tokens //Return localStorage login tokens
var loginTokenString = localStorage.getItem("loginTokens") var loginTokenString = localStorage.getItem("loginTokens");
} }
//Then, check in session storage //Then, check in session storage
if(sessionStorage.getItem("loginTokens") != "null"){ if(sessionStorage.getItem("loginTokens") !== null){
//Return session storage login token //Return session storage login token
var loginTokenString = sessionStorage.getItem("loginTokens") var loginTokenString = sessionStorage.getItem("loginTokens");
} }
//Check if we didn't get any login token //Check if we didn't get any login token

View File

@ -0,0 +1,19 @@
/**
* User informations functions
*
* @author Pierre HUBERT
*/
ComunicWeb.user.getUserInfos = {
/**
* @var {String} User infos cache
*/
/**
* Get user informations
*
* @param {String} userID User on which to make request (current to get connected user)
*/
//getUserInfos
}

View File

@ -5,5 +5,147 @@
*/ */
ComunicWeb.user.userLogin = { ComunicWeb.user.userLogin = {
/**
* @var {Boolean} Store user login state (true by default)
*/
__userLogin: true,
/**
* @var {Integer} Store the current user ID
*/
__userID: 0,
/**
* Tell if user is logged in or not
*
* @return {Boolean} Depend of the result
*/
getUserLoginState: function(){
//Return login state
return this.__userLogin;
},
/**
* Get user ID (if logged in)
*
* @return {String.Boolean} User ID or false if not logged in
*/
getUserID: function(){
//If user is logged in
if(this.getUserLoginState === true){
//Return user ID
return this.__userID;
}
},
/**
* Try to get and store current user ID
*
* @param {function} afterGetCurrentUserID What to do next
* @return {Integer} 0 if it fails
*/
getCurrentUserId: function(afterGetCurrentUserID){
//Prepare and make an API request
var apiURI = "user/getCurrentUserID";
var params = {};
var requireLoginTokens = true;
//What to do after the request is completed
var afterAPIrequest = function(result){
//Check if we got any error
if(result.error){
//Set user ID to 0 (security purpose)
ComunicWeb.user.userLogin.__userID = 0;
//Perform next action
afterGetCurrentUserID(0);
}
else{
//Update user ID
ComunicWeb.user.userLogin.__userID = result.userID;
//Perform next action
afterGetCurrentUserID(result.userID)
}
};
//Perform request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginTokens, afterAPIrequest);
},
/**
* Refresh the user login state
*/
refreshLoginState: function(){
//First, check if we have login tokens
if(ComunicWeb.user.loginTokens.checkLoginTokens() !== true){
//Specify the user isn't logged in
this.__userLogin = false;
this.__userID = 0;
//User not logged in
return false;
}
//Try to use tokens to get user infos
var nextStep = function(userID){
//We check received data
if(userID == 0){
//We consider user is not logged in
ComunicWeb.user.userLogin.__userLogin = false;
}
};
this.getCurrentUserId(nextStep);
},
/**
* Try to login user
*
* @param {String} usermail The mail of user
* @param {String} userpassword The password of the user
* @param {Boolean} permanentLogin Specify wether the login is permanent or not
* @param {function} afterLogin What to do next
*/
loginUser: function(usermail, userpassword, permanentLogin, afterLogin){
//Prepare and make an API request
var apiURI = "user/connectUSER";
var params = {
userMail: usermail,
userPassword: userpassword,
};
//What to do after the request is completed
var afterAPIrequest = function(result){
//Prepare data return
var loginstate = false;
//Check if there is a success message
if(result.success){
loginstate = true;
//Indicates user is logged in
ComunicWeb.user.userLogin.__userLogin = true;
//Store tokens
if(permanentLogin){
var storageType = "local";
}
else {
storageType = "session";
}
ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType);
}
//Perform next action
afterLogin(loginstate);
};
//Perform request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, afterAPIrequest);
}
} }