From 1fb250774224ce2e1f73234347de265042628c41 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 18 Feb 2017 18:26:30 +0100 Subject: [PATCH] Fixed error in login functions and prepared home controller --- assets/js/common/functionsSchema.js | 31 +++++++++++-- assets/js/init.js | 21 ++++++--- assets/js/pages/home/home.js | 26 +++++++++++ assets/js/pagesList.js | 2 +- assets/js/user/loginTokens.js | 4 +- assets/js/user/userLogin.js | 68 +++++++++++++++++++++++++++-- corePage/config/dev.config.php | 3 ++ 7 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 assets/js/pages/home/home.js diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index e43fa7dc..8e00e42b 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -240,18 +240,43 @@ var ComunicWeb = { /** * Refresh the user login state */ - refreshLoginState: function(){}, + refreshLoginState: function(afterLogin){}, /** * Try to login user */ loginUser: function(usermail, userpassword, permanentLogin, afterLogin){}, - + }, + /** * Get user infos */ userInfos: { //TODO: implement - } + }, + }, + + /** + * Pages functions + */ + pages:{ + + /** + * Home page + */ + home: { + /** + * Common homes functions + */ + home:{ + /** + * Open home page + */ + openHomePage: function(additionnalData, targetElement){}, + + + }, + }, + }, } \ No newline at end of file diff --git a/assets/js/init.js b/assets/js/init.js index 7bc46cd0..a051e835 100644 --- a/assets/js/init.js +++ b/assets/js/init.js @@ -25,13 +25,24 @@ ComunicWeb.common.langs.initLanguages(); /** - * Open a page + * What to do after login refresh */ - //Get current page URI - var currentPage = ComunicWeb.common.url.getCurrentWebsiteURL(); + var afterLoginRefresh = function(){ + + /** + * Open a page + */ + //Get current page URI + var currentPage = ComunicWeb.common.url.getCurrentWebsiteURL(); - //Open a page - ComunicWeb.common.page.openPage(currentPage); + //Open a page + ComunicWeb.common.page.openPage(currentPage); + } + + /** + * Get login state + */ + ComunicWeb.user.userLogin.refreshLoginState(afterLoginRefresh); //End of init ComunicWeb.debug.logMessage("Application is ready !"); diff --git a/assets/js/pages/home/home.js b/assets/js/pages/home/home.js new file mode 100644 index 00000000..e8738835 --- /dev/null +++ b/assets/js/pages/home/home.js @@ -0,0 +1,26 @@ +/** + * Common homes functions + */ +ComunicWeb.pages.home.home = { + /** + * Open home page + * @param {Object} additionnalData Additionnal data passed in the method + * @param {element} targetElement Where the template will be applied + * @returns {Boolean} False if it fails + */ + openHomePage: function(additionnalData, targetElement){ + //Check wether if user is logged in or not + var userLoggedIn = ComunicWeb.user.userLogin.getUserLoginState(); + + //Dev feature : Show result + if(userLoggedIn){ + targetElement.appendChild(ComunicWeb.common.messages.createCalloutElem("", "User logged in !", "info")); + } + else{ + targetElement.appendChild(ComunicWeb.common.messages.createCalloutElem("", "User not logged in !", "warning")); + } + + //Everything seems to be OK + return true; + } +}; \ No newline at end of file diff --git a/assets/js/pagesList.js b/assets/js/pagesList.js index a7ce5e07..50b1ca68 100644 --- a/assets/js/pagesList.js +++ b/assets/js/pagesList.js @@ -10,7 +10,7 @@ ComunicWeb.pagesList = { */ home: { pageTitle: "Comunic, v2", - + methodHandler: "ComunicWeb.pages.home.home.openHomePage", }, /** diff --git a/assets/js/user/loginTokens.js b/assets/js/user/loginTokens.js index 78789dd3..284c8ba7 100644 --- a/assets/js/user/loginTokens.js +++ b/assets/js/user/loginTokens.js @@ -42,13 +42,13 @@ ComunicWeb.user.loginTokens = { */ checkLoginTokens: function(){ //First, check in local storage - if(localStorage.getItem("loginTokens") != "null"){ + if(localStorage.getItem("loginTokens") != null){ //It is OK return true; } //Check if we have to remove any thing in session storage - if(sessionStorage.getItem("loginTokens") != "null"){ + if(sessionStorage.getItem("loginTokens") != null){ //It is OK return true; } diff --git a/assets/js/user/userLogin.js b/assets/js/user/userLogin.js index a1ec7f22..572fd676 100644 --- a/assets/js/user/userLogin.js +++ b/assets/js/user/userLogin.js @@ -34,7 +34,7 @@ ComunicWeb.user.userLogin = { */ getUserID: function(){ //If user is logged in - if(this.getUserLoginState === true){ + if(this.getUserLoginState() === true){ //Return user ID return this.__userID; } @@ -59,6 +59,15 @@ ComunicWeb.user.userLogin = { //Set user ID to 0 (security purpose) ComunicWeb.user.userLogin.__userID = 0; + //If error is 404, make user as logged out + if(result.error.code == 401){ + ComunicWeb.user.userLogin.__userLogin = false; + ComunicWeb.user.loginTokens.deleteLoginTokens(); + + //Refresh the page + location.href = document.location; + } + //Perform next action afterGetCurrentUserID(0); } @@ -78,14 +87,21 @@ ComunicWeb.user.userLogin = { /** * Refresh the user login state + * + * @param {Function} afterRefresh Optionnal, what to do after refreshing login */ - refreshLoginState: function(){ + refreshLoginState: function(afterRefresh){ //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; + //If there is a next action, do it (even if user isn't logged in) + if(afterRefresh){ + afterRefresh(); + } + //User not logged in return false; } @@ -97,6 +113,11 @@ ComunicWeb.user.userLogin = { //We consider user is not logged in ComunicWeb.user.userLogin.__userLogin = false; } + + //If there is a next action, do it + if(afterRefresh){ + afterRefresh(); + } }; this.getCurrentUserId(nextStep); }, @@ -126,6 +147,9 @@ ComunicWeb.user.userLogin = { if(result.success){ loginstate = true; + //Log + ComunicWeb.debug.logMessage("User login " + usermail + " successful !"); + //Indicates user is logged in ComunicWeb.user.userLogin.__userLogin = true; @@ -145,7 +169,43 @@ ComunicWeb.user.userLogin = { }; //Perform request - ComunicWeb.common.api.makeAPIrequest(apiURI, params, afterAPIrequest); + ComunicWeb.common.api.makeAPIrequest(apiURI, params, false, afterAPIrequest); + }, - } + /** + * Logout user + * + * @param {Function} afterLogout What to do once user is logged out + */ + logoutUser: function(afterLogout){ + //Prepare and make an API request + var apiURI = "user/disconnectUSER"; + var params = {}; + + //What to do after the request is completed + var afterAPIrequest = function(result){ + + //Log + ComunicWeb.debug.logMessage("Logout request on server terminated."); + + //Perform next action (if specified) + if(afterLogout){ + afterLogout(); + } + + }; + + //Perform request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, afterAPIrequest); + + //Destroy login tokens + ComunicWeb.user.loginTokens.deleteLoginTokens(); + + //Specify user is logged out + this.__userID = 0; + this.__userLogin = false; + + //Done ! + return 0; + }, } \ No newline at end of file diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index f0a1bdd3..06538c6d 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -47,6 +47,9 @@ $config['JSfiles'] = array( "%PATH_ASSETS%js/user/loginTokens.js", "%PATH_ASSETS%js/user/userLogin.js", + //Pages scripts + "%PATH_ASSETS%js/pages/home/home.js", + //Init script "%PATH_ASSETS%js/init.js", );