Connected menuBar login form to login page

This commit is contained in:
Pierre 2017-05-24 17:13:38 +02:00
parent 72fe4108ba
commit 5c5a65cdff
3 changed files with 136 additions and 91 deletions

View File

@ -10,6 +10,11 @@ var ComunicWeb = {
*/ */
__config: ComunicConfig, __config: ComunicConfig,
/**
* List of available pages
*/
pagesList:{},
/** /**
* Common functions * Common functions
*/ */
@ -386,6 +391,13 @@ var ComunicWeb = {
* Perform user login * Perform user login
*/ */
loginSubmit: function(){}, loginSubmit: function(){},
/**
* Display login error message
*
* @return {Boolean} True for a success
*/
displayLoginError: function(){},
}, },
/** /**
@ -399,4 +411,4 @@ var ComunicWeb = {
}, },
}, },
} };

View File

@ -24,6 +24,11 @@ ComunicWeb.components.menuBar.notAuthenticated = {
loginForm.onsubmit = function(){ loginForm.onsubmit = function(){
//Try to login user; in case of failure redirect to login page //Try to login user; in case of failure redirect to login page
//TODO : implement
//In case of error
ComunicWeb.common.page.openPage("login", {
loginFailedMessage: true,
});
return false; return false;
} }
@ -37,6 +42,7 @@ ComunicWeb.components.menuBar.notAuthenticated = {
emailInput.className = "form-control"; emailInput.className = "form-control";
emailInput.placeholder = "Email address"; emailInput.placeholder = "Email address";
emailInput.type = "email"; emailInput.type = "email";
emailInput.value = ComunicWeb.components.mailCaching.get();
//Add password formGroup //Add password formGroup
var passwordFormGroup = createElem("div", loginForm); var passwordFormGroup = createElem("div", loginForm);

View File

@ -5,113 +5,140 @@
*/ */
ComunicWeb.pages.login = { ComunicWeb.pages.login = {
/** /**
* Open login page * Open login page
* *
* @param {Object} additionnalData Additionnal data passed in the method * @param {Object} additionnalData Additionnal data passed in the method
* @param {element} targetElement Where the template will be applied * @param {element} targetElement Where the template will be applied
* @returns {Boolean} False if it fails * @returns {Boolean} False if it fails
*/ */
openLoginPage: function(additionnalData, targetElement){ openLoginPage: function(additionnalData, targetElement){
//First, check if user is already logged in or not //First, check if user is already logged in or not
if(ComunicWeb.user.userLogin.getUserLoginState() === true){ if(ComunicWeb.user.userLogin.getUserLoginState() === true){
//Log message //Log message
ComunicWeb.debug.logMessage("Couldn't open login page because user is already logged in !"); ComunicWeb.debug.logMessage("Couldn't open login page because user is already logged in !");
//Open home page //Open home page
ComunicWeb.common.page.openPage("home"); ComunicWeb.common.page.openPage("home");
//Quit page //Quit page
return false; return false;
} }
//Prepare additional data //Prepare additional data
var additionalData = {}; var additionalTemplateData = {};
//Preparing next actions //Preparing next actions
var afterParsingTemplate = function(){ var afterParsingTemplate = function(){
//Change body class name //Change body class name
document.body.className = "login-page hold-transition"; document.body.className = "login-page hold-transition";
//Enable iCheck //Enable iCheck
$(function () { $(function () {
$('input').iCheck({ $('input').iCheck({
checkboxClass: 'icheckbox_square-blue', checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue', radioClass: 'iradio_square-blue',
increaseArea: '20%' // optional increaseArea: '20%' // optional
}); });
}); });
//Get login form element //Define email address (if possible)
var loginBody = document.getElementById("loginForm"); byId("usermail").value = ComunicWeb.components.mailCaching.get();
//Get login button //Get login form element
var loginButton = loginBody.getElementsByClassName("btn-login")[0]; var loginBody = document.getElementById("loginForm");
//Make the login action accessible //Get login button
//loginButton.onclick = ComunicWeb.pages.login.loginSubmit; var loginButton = loginBody.getElementsByClassName("btn-login")[0];
loginBody.onsubmit = ComunicWeb.pages.login.loginSubmit;
};
//Apply template //Make the login action accessible
ComunicWeb.common.page.getAndShowTemplate(targetElement, additionalData, "pages/login/loginPage.tpl", afterParsingTemplate, true); //loginButton.onclick = ComunicWeb.pages.login.loginSubmit;
}, loginBody.onsubmit = ComunicWeb.pages.login.loginSubmit;
/** //Check if additionnal data were specified
* Perform user login if(additionnalData){
* //Check if we have to display a login failed message
* @return {Boolean} False if it fails if(additionnalData.loginFailedMessage)
*/ ComunicWeb.pages.login.displayLoginError();
loginSubmit: function(){ }
//Get inputs };
var usermailInput = document.getElementById("usermail"); //Usermail
var userpasswordInput = document.getElementById("userpassword"); //Password
var rememberLoginInput = document.getElementById("rememberLogin"); //Remember login
//Check inputs //Apply template
if(!( ComunicWeb.common.page.getAndShowTemplate(targetElement, additionalTemplateData, "pages/login/loginPage.tpl", afterParsingTemplate, true);
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");
//Stop function execution /**
return false; * Perform user login
} *
* @return {Boolean} False if it fails
*/
loginSubmit: function(){
//Get inputs
var usermailInput = byId("usermail"); //Usermail
var userpasswordInput = byId("userpassword"); //Password
var rememberLoginInput = document.getElementById("rememberLogin"); //Remember login
//Enable overlay (use .remove() to remove) //Check inputs
var screenOverlay = ComunicWeb.common.page.showTransparentWaitSplashScreen(); if(!(
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");
//Retrieve values //Stop function execution
var usermail = usermailInput.value; return false;
var userpassword = userpasswordInput.value; }
var permanentLogin = rememberLoginInput.checked;
//What to do once user is logged in (or not) //Enable overlay (use .remove() to remove)
var afterTryLogin = function(loginResult){ var screenOverlay = ComunicWeb.common.page.showTransparentWaitSplashScreen();
//First, remove overlay //Retrieve values
screenOverlay.remove(); var usermail = usermailInput.value;
var userpassword = userpasswordInput.value;
var permanentLogin = rememberLoginInput.checked;
//Check if login failed //What to do once user is logged in (or not)
if(!loginResult){ var afterTryLogin = function(loginResult){
//Create error modal
errorMessageElem = ComunicWeb.common.messages.createCalloutElem("Login failed", "Please check your usermail and password", "danger");
//Apply error modal //First, remove overlay
document.getElementById('loginMessagesTarget').innerHTML = ""; screenOverlay.remove();
document.getElementById('loginMessagesTarget').appendChild(errorMessageElem);
//Return false //Save email address
return false; ComunicWeb.components.mailCaching.set(usermail);
}
//Open home page //Check if login failed
ComunicWeb.common.page.openPage("home"); if(!loginResult){
};
//Try to login user //Display error
ComunicWeb.user.userLogin.loginUser(usermail, userpassword, permanentLogin, afterTryLogin); ComunicWeb.pages.login.displayLoginError();
},
//Return false
return false;
}
//Open home page
ComunicWeb.common.page.openPage("home");
};
//Try to login user
ComunicWeb.user.userLogin.loginUser(usermail, userpassword, permanentLogin, afterTryLogin);
},
/**
* Display login error message
*
* @return {Boolean} True for a success
*/
displayLoginError: function(){
//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);
//Success
return true;
},
}; };