From 82b009bdcca1609a91e92ca8f41f01093e4cdd04 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 29 Jan 2017 15:49:21 +0100 Subject: [PATCH] Added login system --- assets/js/common/api.js | 25 ++++- assets/js/common/errors.js | 3 +- assets/js/common/functionsSchema.js | 44 ++++++++- assets/js/user/loginTokens.js | 10 +- assets/js/user/userInfos.js | 19 ++++ assets/js/user/userLogin.js | 144 +++++++++++++++++++++++++++- 6 files changed, 232 insertions(+), 13 deletions(-) create mode 100644 assets/js/user/userInfos.js diff --git a/assets/js/common/api.js b/assets/js/common/api.js index b2e38b64..7ca22be9 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -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); } } diff --git a/assets/js/common/errors.js b/assets/js/common/errors.js index 78e6fdc8..01bcad87 100644 --- a/assets/js/common/errors.js +++ b/assets/js/common/errors.js @@ -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); } /** diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index f6f38266..e43fa7dc 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -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 - }, + } }, } \ No newline at end of file diff --git a/assets/js/user/loginTokens.js b/assets/js/user/loginTokens.js index d2b2745a..78789dd3 100644 --- a/assets/js/user/loginTokens.js +++ b/assets/js/user/loginTokens.js @@ -10,7 +10,7 @@ ComunicWeb.user.loginTokens = { /** * Set User tokens * - * @param {Object} tokens The token object + * @param {Object} tokens The tokens object * @param {Type} storageType The token destination (local or session) */ setUserTokens: function(tokens, storageType){ @@ -64,15 +64,15 @@ ComunicWeb.user.loginTokens = { */ getLoginTokens: function(){ //First, check in local storage - if(localStorage.getItem("loginTokens") != "null"){ + if(localStorage.getItem("loginTokens") !== null){ //Return localStorage login tokens - var loginTokenString = localStorage.getItem("loginTokens") + var loginTokenString = localStorage.getItem("loginTokens"); } //Then, check in session storage - if(sessionStorage.getItem("loginTokens") != "null"){ + if(sessionStorage.getItem("loginTokens") !== null){ //Return session storage login token - var loginTokenString = sessionStorage.getItem("loginTokens") + var loginTokenString = sessionStorage.getItem("loginTokens"); } //Check if we didn't get any login token diff --git a/assets/js/user/userInfos.js b/assets/js/user/userInfos.js new file mode 100644 index 00000000..2ec01583 --- /dev/null +++ b/assets/js/user/userInfos.js @@ -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 +} \ No newline at end of file diff --git a/assets/js/user/userLogin.js b/assets/js/user/userLogin.js index a28e6907..a1ec7f22 100644 --- a/assets/js/user/userLogin.js +++ b/assets/js/user/userLogin.js @@ -5,5 +5,147 @@ */ 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); + + } } \ No newline at end of file