(function() { "use strict"; angular.module("agora.plus.services.LoginService", []) .service("LoginService", [ "$route", "$timeout", "$q", "$http", LoginService ]); /** * [LoginService Service to be used to deal with all login/logout related features] * @param {[Object]} $timeout [Angular $timeout service] * @param {[Object]} $q [Angular $q service] * @param {[Object]} $http [Angular $http service] */ function LoginService($route, $timeout, $q, $http) { var vm = this; vm.baseUrlApiDashboard = PCK_GLOBAL_VARIABLES.webservice_path_API_DASHBOARD; return { validateLogin: function($scope, constant, validateLogin){ try { if (validateLogin) { var responseData = {}, validContexts = ["BACKOFFICE", "FRONTOFFICE", "TEMPORARY"], responseType = parseInt(validateLogin.TYPE_RESULT, 10); $scope.imgPath = constant.imgPath; $scope.validAuthentication = false; if (responseType > 0) { responseData = validateLogin.DATA; if (responseData.hasOwnProperty("APP_CONTEXT")) { if (validContexts.indexOf(responseData.APP_CONTEXT) >= 0) { $scope.validAuthentication = responseData.APP_CONTEXT === "BACKOFFICE"; } } } else { throw(validateLogin.ERROR.MESSAGE); } } else { $scope.validAuthentication = false; } } catch(error) { this.logout(); // window.location = PCK_GLOBAL_VARIABLES.client_home_view; } }, /** * [setNextRoute Set visited URL] * @param {[String]} path [The URL] */ setNextRoute: function(path) { sessionHandler.sessionStorage.set("nextRoute", path); }, /** * [getNextRoute Get the visited URL] * @return {[String]} [Visited URL] */ getNextRoute: function() { return sessionHandler.sessionStorage.get("nextRoute"); }, /** * [deleteNextRoute Delete from local storage the last visited route] */ deleteNextRoute: function() { sessionHandler.sessionStorage.removeItem("nextRoute"); }, /** * [redirectToVisitedUrl After login, redirect to visited URL] */ redirectToVisitedUrl: function() { var path = this.getNextRoute(); if (path !== null) { window.location = path; } }, /** * [redirectToHome Redirect the user to ] */ redirectToHome: function() { if (sessionHandler.sessionStorage.get("showBridge") !== null && sessionHandler.sessionStorage.get("showBridge") === true) { if (sessionHandler.sessionStorage.get("applicationContext") === "FRONT") { window.location = PCK_GLOBAL_VARIABLES.dashboard_v2; } else { window.location = PCK_GLOBAL_VARIABLES.base_url_bo; } } else { window.location = PCK_GLOBAL_VARIABLES.pck_home_home_view; } }, /** * [showPopup Trigger the login module window on called in the package client home] */ showPopup: function() { $timeout(function() { var elem = angular.element(document.querySelector("#login-link")); if (elem.length > 0) { // throw("Element not found"); elem[0].click(); } }, 0, false); }, /** * [isLoggedIn Check if the user is logged in based on session id saved in sessionStorage] * @return {Boolean} [Trus if is logged in false otherwise] */ isLoggedIn: function() { if (sessionHandler.sessionStorage.get("SESSION_ID") != null && sessionHandler.sessionStorage.get("SESSION_ID") !== "") { return true; } return false; }, /** * [logout Logout the user from the application] */ logout: function() { var sessionObj = sessionHandler.sessionStorage.get("sessionObj"), _self = this; logOut().then(function(data) { }).finally(function() { if (sessionObj !== null ) { // Invalidate cookies // document.cookie = "session_id=" + sessionStorage.SESSION_ID + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC" + ";path=" + sessionObj.urlDad; // document.cookie = "session_id=" + sessionStorage.SESSION_ID + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC" + ";path=" + sessionObj.webServicePath; // document.cookie = "ref_year=" + sessionObj.schoolYear + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC" + ";path=" + sessionObj.urlDad; // document.cookie = "ref_year=" + sessionObj.schoolYear + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC" + ";path=" + sessionObj.webServicePath; document.cookie = "session_id=" + sessionHandler.sessionStorage.get("SESSION_ID") + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC"; document.cookie = "ref_year=" + sessionObj.schoolYear + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC"; document.cookie = "portail_session_id=" + sessionHandler.sessionStorage.get("SESSION_ID") + ";expires=" + "Thu, 01 Jan 1970 00:00:00 UTC"; } // Clear sessionStorage sessionHandler.localStorage.clear(); sessionHandler.sessionStorage.clear(); sessionHandler.sessionStorage.set("SESSION_ID", ""); _self.redirectToHome(); }).catch(function(error) { console.error(error); }); } }; /** * [logOut Clear authentication cookies on server side] * @return {[Object]} [$q promise] */ function logOut() { var defer = $q.defer(), responseResult; $http.post(vm.baseUrlApiDashboard + "logout", { SESSION_ID: sessionHandler.sessionStorage.get("SESSION_ID") }) .then(function(response) { responseResult = parseInt(response.data.TYPE_RESULT, 10); if (responseResult >= 0) { defer.resolve(response.data); } else { defer.reject(response.data.ERROR.MESSAGE); } }) .catch(function(error) { defer.reject(error); }); return defer.promise; } } }).call();