diff --git a/README.md b/README.md index 90d7b3e8..47f7ef0c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # WEB-WebComunicApp -WebComunic RestClient \ No newline at end of file +WebComunic RestClient diff --git a/README.md.bak b/README.md.bak new file mode 100644 index 00000000..90d7b3e8 --- /dev/null +++ b/README.md.bak @@ -0,0 +1,3 @@ +# WEB-WebComunicApp + +WebComunic RestClient \ No newline at end of file diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 135e18a5..f0a203ce 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -335,7 +335,17 @@ var ComunicWeb = { * Perform user login */ loginSubmit: function(){}, - } + }, + + /** + * Logout controller + */ + logout: { + /** + * Open logout page and perform logout + */ + openLogoutPage: function(additionnalData, targetElement){}, + }, }, } \ No newline at end of file diff --git a/assets/js/common/network.js b/assets/js/common/network.js index 2edc2b72..04a5ed73 100644 --- a/assets/js/common/network.js +++ b/assets/js/common/network.js @@ -4,7 +4,6 @@ * @author Pierre HUBERT */ - ComunicWeb.common.network = { /** diff --git a/assets/js/common/page.js b/assets/js/common/page.js index 31f3e01e..3aecabc1 100644 --- a/assets/js/common/page.js +++ b/assets/js/common/page.js @@ -92,13 +92,18 @@ ComunicWeb.common.page = { //Extract the first part of the URL var firstPartURI = pageURI.toString(); + //Check if there are hashtag for the URL + if(firstPartURI.indexOf("#") != -1){ + firstPartURI = firstPartURI.split("#")[0]; + } + //Check if pageURI is empty if(firstPartURI == ""){ firstPartURI = "home"; } //Check if there is also subfolders - if(firstPartURI.indexOf("/") != "/"){ + if(firstPartURI.indexOf("/") != -1){ firstPartURI = firstPartURI.split("/")[0]; } diff --git a/assets/js/common/url.js b/assets/js/common/url.js index ad6fe604..f87d489d 100644 --- a/assets/js/common/url.js +++ b/assets/js/common/url.js @@ -32,9 +32,9 @@ ComunicWeb.common.url = { //Determine the new URL var newURL = ComunicWeb.__config.siteURL + newURI; - + //Apply it - window.history.pushState("object or string", newTitle, newURI); + window.history.pushState("object or string", newTitle, newURL); //Everything is OK return true; diff --git a/assets/js/pages/home/home.js b/assets/js/pages/home/home.js index 79c6ebe8..9b4589e9 100644 --- a/assets/js/pages/home/home.js +++ b/assets/js/pages/home/home.js @@ -18,9 +18,25 @@ ComunicWeb.pages.home.home = { //Dev feature : Show result if(userLoggedIn){ targetElement.appendChild(ComunicWeb.common.messages.createCalloutElem("", "User logged in !", "info")); + + //Create logout button + var loginButton = document.createElement("button"); + loginButton.onclick = (function(){ + ComunicWeb.common.page.openPage("logout"); + }); + loginButton.innerHTML="Logout"; + targetElement.appendChild(loginButton); } else{ targetElement.appendChild(ComunicWeb.common.messages.createCalloutElem("", "User not logged in !", "warning")); + + //Create login button + var loginButton = document.createElement("button"); + loginButton.onclick = (function(){ + ComunicWeb.common.page.openPage("login"); + }); + loginButton.innerHTML="Login"; + targetElement.appendChild(loginButton); } //Everything seems to be OK diff --git a/assets/js/pages/login.js b/assets/js/pages/login.js index 996cd00a..bbcfa1fb 100644 --- a/assets/js/pages/login.js +++ b/assets/js/pages/login.js @@ -48,7 +48,9 @@ ComunicWeb.pages.login = { //Get login button var loginButton = loginBody.getElementsByClassName("btn-login")[0]; - loginButton.onclick=ComunicWeb.pages.login.loginSubmit; + //Make the login action accessible + //loginButton.onclick = ComunicWeb.pages.login.loginSubmit; + loginBody.onsubmit = ComunicWeb.pages.login.loginSubmit; }; //Apply template @@ -62,14 +64,14 @@ ComunicWeb.pages.login = { */ loginSubmit: function(){ //Get inputs - var usermail = document.getElementById("usermail"); //Usermail - var userpassword = document.getElementById("userpassword"); //Password - var rememberLogin = document.getElementById("rememberLogin"); //Remember login + var usermailInput = document.getElementById("usermail"); //Usermail + var userpasswordInput = document.getElementById("userpassword"); //Password + var rememberLoginInput = document.getElementById("rememberLogin"); //Remember login //Check inputs if(!( - ComunicWeb.common.formChecker.checkInput(usermail, true) && //Check usermail input - ComunicWeb.common.formChecker.checkInput(userpassword, true) //Check password input + ComunicWeb.common.formChecker.checkInput(usermailInput, true) && //Check usermail input + ComunicWeb.common.formChecker.checkInput(userpasswordInput, true) //Check password input )){ //Error notification ComunicWeb.common.notificationSystem.showNotification("Please check what you've typed !", "error"); @@ -78,7 +80,38 @@ ComunicWeb.pages.login = { return false; } + //Enable overlay (use .remove() to remove) + var screenOverlay = ComunicWeb.common.page.showTransparentWaitSplashScreen(); + + //Retrieve values + var usermail = usermailInput.value; + var userpassword = userpasswordInput.value; + var permanentLogin = rememberLoginInput.checked; - var overlay = ComunicWeb.common.page.showTransparentWaitSplashScreen(); + //What to do once user is logged in (or not) + var afterTryLogin = function(loginResult){ + + //First, remove overlay + screenOverlay.remove(); + + //Check if login failed + if(!loginResult){ + //Create error modal + errorMessageElem = ComunicWeb.common.messages.createCalloutElem("Login failed", "Please check your usermail and password", "danger"); + + //Apply error modal + document.getElementById('loginMessagesTarget').innerHTML = ""; + document.getElementById('loginMessagesTarget').appendChild(errorMessageElem); + + //Return false + return false; + } + + //Open home page + ComunicWeb.common.page.openPage("home"); + }; + + //Try to login user + ComunicWeb.user.userLogin.loginUser(usermail, userpassword, permanentLogin, afterTryLogin); }, }; \ No newline at end of file diff --git a/assets/js/pages/logout.js b/assets/js/pages/logout.js new file mode 100644 index 00000000..33428076 --- /dev/null +++ b/assets/js/pages/logout.js @@ -0,0 +1,30 @@ +/** + * Logout page main controller + * + * @author Pierre HUBERT + */ + +ComunicWeb.pages.logout = { + + /** + * Open logout page and perform logout + * + * @param {Object} additionnalData Additionnal data passed in the method + * @param {element} targetElement Where the template will be applied + * @returns {Boolean} False if it fails + */ + openLogoutPage: function(additionnalData, targetElement){ + //Enable screen overlay + var screenOverlay = ComunicWeb.common.page.showTransparentWaitSplashScreen(); + + //Perform logout + ComunicWeb.user.userLogin.logoutUser(); + + //Show a success notification + ComunicWeb.common.notificationSystem.showNotification("Good bye, you were sucessfully logouted !", "sucess", 5); + + //Open login page + ComunicWeb.common.page.openPage("login"); + }, + +}; \ No newline at end of file diff --git a/assets/js/pagesList.js b/assets/js/pagesList.js index 8e77d1e7..8e68c588 100644 --- a/assets/js/pagesList.js +++ b/assets/js/pagesList.js @@ -21,6 +21,14 @@ ComunicWeb.pagesList = { methodHandler: "ComunicWeb.pages.login.openLoginPage", }, + /** + * Logout page + */ + logout: { + pageTitle: "Logout", + methodHandler: "ComunicWeb.pages.logout.openLogoutPage", + }, + /** * 404 Page not found */ diff --git a/assets/js/user/userLogin.js b/assets/js/user/userLogin.js index 572fd676..b5d7167c 100644 --- a/assets/js/user/userLogin.js +++ b/assets/js/user/userLogin.js @@ -163,9 +163,18 @@ ComunicWeb.user.userLogin = { ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType); } - //Perform next action - afterLogin(loginstate); + //Perform next action if login failed + if(!loginstate) { + afterLogin(loginstate); + return false; + } + //Else refresh login state to get user ID + ComunicWeb.user.userLogin.refreshLoginState(function(){ + //And then we'll be able to perform next action + afterLogin(true); + }); + }; //Perform request diff --git a/assets/templates/pages/login/loginPage.tpl b/assets/templates/pages/login/loginPage.tpl index 2b7c3c28..4e93d74e 100644 --- a/assets/templates/pages/login/loginPage.tpl +++ b/assets/templates/pages/login/loginPage.tpl @@ -8,7 +8,10 @@

Login to your Comunic account.

-
+ +
+ +
@@ -31,7 +34,7 @@
- + diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index 831eaac1..a16f3da9 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -55,6 +55,7 @@ $config['JSfiles'] = array( //Pages scripts "%PATH_ASSETS%js/pages/home/home.js", "%PATH_ASSETS%js/pages/login.js", + "%PATH_ASSETS%js/pages/logout.js", //Init script "%PATH_ASSETS%js/init.js",