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 {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
*/
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginTokens, nextAction){
//Prepare the request URL
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
var count = 0;
var datas = "";
@ -27,7 +38,7 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
count++; //Increment counter
}
}
//Create request
var apiXHR = new XMLHttpRequest();
@ -38,10 +49,16 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
//We continue only if request is terminated
if(apiXHR.readyState == 4){
//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
nextAction(apiXHR.responseText);
nextAction(result);
}
}

View File

@ -21,12 +21,13 @@ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCo
"errorCode": errorCode,
"errorData": errorData.toSource(),
}
var requireLoginToken = false;
//Not any next action for now
nextAction = function(){};
//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
*/
makeAPIrequest: function(apiURI, params, nextAction){},
makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){},
},
/**
@ -211,7 +211,47 @@ var ComunicWeb = {
* Manage user login
*/
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
},
}
},
}