Fixed error in login functions and prepared home controller

This commit is contained in:
Pierre 2017-02-18 18:26:30 +01:00
parent 82b009bdcc
commit 1fb2507742
7 changed files with 140 additions and 15 deletions

View File

@ -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){},
},
},
},
}

View File

@ -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
ComunicWeb.common.page.openPage(currentPage);
/**
* Open a page
*/
//Get current page URI
var currentPage = ComunicWeb.common.url.getCurrentWebsiteURL();
//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 !");

View File

@ -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;
}
};

View File

@ -10,7 +10,7 @@ ComunicWeb.pagesList = {
*/
home: {
pageTitle: "Comunic, v2",
methodHandler: "ComunicWeb.pages.home.home.openHomePage",
},
/**

View File

@ -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;
}

View File

@ -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;
},
}

View File

@ -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",
);