Update login page

This commit is contained in:
Pierre HUBERT 2021-02-13 15:14:45 +01:00
parent 75f926d0c6
commit 9dfe0e39eb
4 changed files with 93 additions and 208 deletions

View File

@ -364,54 +364,6 @@ var ComunicWeb = {
*/ */
deleteLoginTokens: function(){}, deleteLoginTokens: function(){},
}, },
/**
* 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(afterLogin){},
/**
* Try to login user
*/
loginUser: function(usermail, userpassword, permanentLogin, afterLogin){},
/**
* Logout user
*/
logoutUser: function(afterLogout){},
//TODO : implement
},
/** /**
* Get user infos * Get user infos

View File

@ -59,38 +59,29 @@ ComunicWeb.common.system = {
ComunicWeb.components.darkTheme.refresh(); ComunicWeb.components.darkTheme.refresh();
/** /**
* What to do after login refresh * Get login state
*/ */
var afterLoginRefresh = async function(){ await UserLogin.refreshLoginState();
// Initialize Websocket if user is connected
if(signed_in()) {
await UserWebSocket.Connect();
await UserWebSocket.WaitForConnected();
}
/** // Initialize Websocket if user is connected
* Open a page if(signed_in()) {
*/ await UserWebSocket.Connect();
if(!openPage){ await UserWebSocket.WaitForConnected();
//Refresh current page
ComunicWeb.common.page.refresh_current_page();
}
else
//Open specified page
ComunicWeb.common.page.openPage(openPage);
//End of init
ComunicWeb.debug.logMessage("Application is ready !");
} }
/** /**
* Get login state * Open a page
*/ */
ComunicWeb.user.userLogin.refreshLoginState(afterLoginRefresh); if(!openPage){
//Refresh current page
ComunicWeb.common.page.refresh_current_page();
}
else
//Open specified page
ComunicWeb.common.page.openPage(openPage);
//Success //End of init
return true; ComunicWeb.debug.logMessage("Application is ready !");
}, },
/** /**

View File

@ -67,7 +67,6 @@ ComunicWeb.pages.login = {
var loginButton = loginBody.getElementsByClassName("btn-login")[0]; var loginButton = loginBody.getElementsByClassName("btn-login")[0];
//Make the login action accessible //Make the login action accessible
//loginButton.onclick = ComunicWeb.pages.login.loginSubmit;
loginBody.onsubmit = ComunicWeb.pages.login.loginSubmit; loginBody.onsubmit = ComunicWeb.pages.login.loginSubmit;
@ -103,9 +102,7 @@ ComunicWeb.pages.login = {
)){ )){
//Error notification //Error notification
ComunicWeb.common.notificationSystem.showNotification(lang("_login_page_bad_input"), "error"); ComunicWeb.common.notificationSystem.showNotification(lang("_login_page_bad_input"), "error");
return;
//Stop function execution
return false;
} }
//Enable overlay (use .remove() to remove) //Enable overlay (use .remove() to remove)
@ -117,7 +114,7 @@ ComunicWeb.pages.login = {
var permanentLogin = rememberLoginInput.checked; var permanentLogin = rememberLoginInput.checked;
//What to do once user is logged in (or not) //What to do once user is logged in (or not)
var afterTryLogin = function(loginResult){ var afterTryLogin = (loginResult) => {
//First, remove overlay //First, remove overlay
screenOverlay.remove(); screenOverlay.remove();

View File

@ -4,7 +4,7 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
ComunicWeb.user.userLogin = { const UserLogin = {
/** /**
* @var {Boolean} Store user login state (true by default) * @var {Boolean} Store user login state (true by default)
@ -28,8 +28,7 @@ ComunicWeb.user.userLogin = {
* *
* @return {Boolean} Depend of the result * @return {Boolean} Depend of the result
*/ */
getUserLoginState: function(){ getUserLoginState: function() {
//Return login state
return this.__userLogin; return this.__userLogin;
}, },
@ -38,100 +37,66 @@ ComunicWeb.user.userLogin = {
* *
* @return {String.Boolean} User ID or false if not logged in * @return {String.Boolean} User ID or false if not logged in
*/ */
getUserID: function(){ getUserID: function() {
//If user is logged in //If user is logged in
if(this.getUserLoginState() === true){ if(this.getUserLoginState() === true){
//Return user ID
return this.__userID; return this.__userID;
} }
}, },
/** /**
* Try to get and store current user ID * Try to get and store current user ID
*
* @param {function} afterGetCurrentUserID What to do next
* @return {Integer} 0 if it fails
*/ */
getCurrentUserId: function(afterGetCurrentUserID){ async getCurrentUserId(){
//Prepare and make an API request try {
var apiURI = "user/getCurrentUserID"; const result = await api("account/id", {}, true);
var params = {};
var requireLoginTokens = true;
//What to do after the request is completed // Update user ID
var afterAPIrequest = function(result){ ComunicWeb.user.userLogin.__userID = result.userID;
//Check if we got any error
if(result.error){
//Set user ID to 0 (security purpose)
ComunicWeb.user.userLogin.__userID = 0;
//If error is 412, make user as logged out //Notify about the event
if(result.error.code == 412){ SendEvent("got_user_id", {
ComunicWeb.user.userLogin.__userLogin = false; userID: result.userID
ComunicWeb.user.loginTokens.deleteLoginTokens(); });
//Restart the application
ComunicWeb.common.system.restart();
}
//Perform next action } catch(e) {
afterGetCurrentUserID(0);
}
else
{
//Update user ID
ComunicWeb.user.userLogin.__userID = result.userID;
//Perform next action //Set user ID to 0 (security purpose)
afterGetCurrentUserID(result.userID); ComunicWeb.user.userLogin.__userID = 0;
//Notify about the event //If error is 412, make user as logged out
SendEvent("got_user_id", { if(result.error.code == 412) {
userID: result.userID ComunicWeb.user.userLogin.__userLogin = false;
}); ComunicWeb.user.loginTokens.deleteLoginTokens();
//Restart the application
ComunicWeb.common.system.restart();
} }
}; }
//Perform request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginTokens, afterAPIrequest);
}, },
/** /**
* Refresh the user login state * Refresh the user login state
*
* @param {Function} afterRefresh Optionnal, what to do after refreshing login
*/ */
refreshLoginState: function(afterRefresh){ refreshLoginState: async function() {
//First, check if we have login tokens // First, check if we have login tokens
if(ComunicWeb.user.loginTokens.checkLoginTokens() !== true){ if(ComunicWeb.user.loginTokens.checkLoginTokens() !== true) {
//Specify the user isn't logged in //Specify the user isn't logged in
this.__userLogin = false; this.__userLogin = false;
this.__userID = 0; this.__userID = 0;
//If there is a next action, do it (even if user isn't logged in) return;
if(afterRefresh){
afterRefresh();
}
//User not logged in
return false;
} }
//Try to use tokens to get user infos // Try to use tokens to get user infos
var nextStep = function(userID){ await UserLogin.getCurrentUserId();
//We check received data
if(userID == 0){
//We consider user is not logged in
ComunicWeb.user.userLogin.__userLogin = false;
}
//If there is a next action, do it //We check received data
if(afterRefresh){ if(this.__userID == 0){
afterRefresh(); // We consider user is not logged in
} ComunicWeb.user.userLogin.__userLogin = false;
}; }
this.getCurrentUserId(nextStep);
}, },
/** /**
@ -142,74 +107,52 @@ ComunicWeb.user.userLogin = {
* @param {Boolean} permanentLogin Specify wether the login is permanent or not * @param {Boolean} permanentLogin Specify wether the login is permanent or not
* @param {function} afterLogin What to do next * @param {function} afterLogin What to do next
*/ */
loginUser: function(usermail, userpassword, permanentLogin, afterLogin){ loginUser: async function(usermail, userpassword, permanentLogin, afterLogin) {
//Prepare and make an API request try {
var apiURI = "user/connectUSER"; const result = await api("account/login", {
var params = { mail: usermail,
userMail: usermail, password: userpassword,
userPassword: userpassword, })
};
//What to do after the request is completed //Log
const afterAPIrequest = async function(result){ ComunicWeb.debug.logMessage("User login " + usermail + " successful !");
//Prepare data return
var loginstate = false; //Indicates user is logged in
ComunicWeb.user.userLogin.__userLogin = true;
//Check if there is a success message //Store tokens
if(result.success){ if(permanentLogin){
loginstate = true; var storageType = "local";
}
else {
storageType = "session";
}
ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType);
//Log // Save email address
ComunicWeb.debug.logMessage("User login " + usermail + " successful !"); ComunicWeb.components.mailCaching.set(usermail);
//Indicates user is logged in // Initialize websocket
ComunicWeb.user.userLogin.__userLogin = true; await UserWebSocket.Connect();
await UserWebSocket.WaitForConnected();
// Else refresh login state to get user ID
await this.refreshLoginState();
//Then get and apply user language settings
ComunicWeb.components.settings.interface.getLanguage(function(lang){
if(!lang.error)
ComunicWeb.common.langs.setLang(lang.lang);
//Store tokens afterLogin(true);
if(permanentLogin){
var storageType = "local";
}
else {
storageType = "session";
}
ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType);
//Save email address
ComunicWeb.components.mailCaching.set(usermail);
// Initialize websocket
await UserWebSocket.Connect();
await UserWebSocket.WaitForConnected();
}
//Perform next action if login failed
if(!loginstate) {
ComunicWeb.user.userLogin._last_attempt_response_code = result.error.code;
afterLogin(loginstate);
return false;
}
//Else refresh login state to get user ID
ComunicWeb.user.userLogin.refreshLoginState(function(){
//Then get and apply user language settings
ComunicWeb.components.settings.interface.getLanguage(function(lang){
if(!lang.error)
ComunicWeb.common.langs.setLang(lang.lang);
//And then we'll be able to perform next action
afterLogin(true);
});
}); });
};
//Perform request } catch(e) {
ComunicWeb.common.api.makeAPIrequest(apiURI, params, false, afterAPIrequest); UserLogin._last_attempt_response_code = e.code;
afterLogin(false);
}
}, },
/** /**
@ -258,4 +201,6 @@ ComunicWeb.user.userLogin = {
get_last_attempt_response_code: function(){ get_last_attempt_response_code: function(){
return this._last_attempt_response_code; return this._last_attempt_response_code;
} }
} }
ComunicWeb.user.userLogin = UserLogin;