<commit_before>"use strict";
define(['app','api'], function (app) {
    
	app.register.controller('RegisterController',['$scope','$rootScope','$window','$cookies','api', function ($scope,$rootScope,$window,$cookies,api) {
		$scope.Register = {};
		$scope.cancel = function(){
			$scope.Register = {};
		}
		$scope.register = function(){
			var data =  $scope.Register;
				$scope.Registering = true;
			api.POST('register',data,function(response){
				$scope.Registering = false;
				if(response.data){
					$cookies.put('__USER',JSON.stringify(response.data));
					$window.location.href="#/";
				}
			});
		}
		
	}]);
	
	
	app.register.controller('LoginController',['$scope','$rootScope','$window','$cookies','api','localStorageService', function ($scope,$rootScope,$window,$cookies,api,$locstor) {
		$rootScope.__SHOW_REG = false;
		if($window.location.hash=='#/logout'){
			$rootScope.__SIDEBAR_OPEN = false;
			$rootScope.__USER=null;
			$cookies.remove('__USER');
			$cookies.remove('__MENUS');
			$locstor.remove('__SIDEBAR_MENUS');
			api.POST('logout',function success(response){
					$rootScope.$emit('UserLoggedOut');
					$window.location.href="#/login";
			});
		}
		if($rootScope.__USER){
			$window.location.href="#/";
		}
		$scope.cancel = function(){
			$scope.loginMessage = null;
			$scope.User = {};
		}
		$scope.cancel();
		$scope.login = function(){
			var data = $scope.User;
			$scope.LoggingIn = true;
			$scope.loginMessage =null;
			api.POST('login',data,function(response){
				$scope.LoggingIn = false;
				if(response.data.user){
					$rootScope.__USER = response.data;
					$cookies.put('__USER',JSON.stringify(response.data));
					$rootScope.__LOGGEDIN  = true;
					$rootScope.$emit('UserLoggedIn');
					$window.location.href="#/";
				}else{
					$scope.loginMessage = response.message;
				}
			});
		}
		
		$rootScope.$on('UserLoggedIn',function(){
			requestModulesList();
			
		});
		
		$rootScope.$on('$routeChangeStart', function (scope, next, current) {
			if($rootScope.__LOGGEDIN)
				if(!$rootScope.__SIDEBAR_MENUS)
					readModuleListCache();
				else
					requestModulesList();
		});
		
		function requestModulesList(){
			api.GET('modules',{limit:'less'},function(response){
				var modules = response.data;
				var menus = [];
				var lastIndex=-1;
				for(var i in modules){
					var mod =  modules[i];
					if(!$rootScope.__USER) break;
					var granted = $rootScope.__USER.user.access.indexOf(mod.id)!==-1 || mod.is_parent;
					if(!mod.is_child){
						//Menu
						if(mod.is_parent)
							mod.children=[];
						if(granted) menus.push(mod);
						lastIndex++;
					}else{
						//Submenu
						if(granted){
							menus[lastIndex].children.push(mod);
						}
							
							
					}
				}
				$locstor.set('__SIDEBAR_MENUS',JSON.stringify(menus));
				readModuleListCache();
			});
		}
		function readModuleListCache(){
				var sidebar = $locstor.get('__SIDEBAR_MENUS');
				var cache = JSON.parse(sidebar);
				$rootScope.__SIDEBAR_MENUS =  cache;
				
		}
	}]);
});<commit_msg>Improve authentication flow<commit_after>"use strict";
define(['app','api'], function (app) {
    
	app.register.controller('RegisterController',['$scope','$rootScope','$window','$cookies','api', function ($scope,$rootScope,$window,$cookies,api) {
		$scope.Register = {};
		$scope.cancel = function(){
			$scope.Register = {};
		}
		$scope.register = function(){
			var data =  $scope.Register;
				$scope.Registering = true;
			api.POST('register',data,function(response){
				$scope.Registering = false;
				if(response.data){
					$cookies.put('__USER',JSON.stringify(response.data));
					$window.location.href="#/";
				}
			});
		}
		
	}]);
	
	
	app.register.controller('LoginController',['$scope','$rootScope','$window','$cookies','api','localStorageService', function ($scope,$rootScope,$window,$cookies,api,$locstor) {
		var isModuleListRequested;
		$rootScope.__SHOW_REG = false;
		if($window.location.hash=='#/logout'){
			$rootScope.__SIDEBAR_OPEN = false;
			$rootScope.__USER=null;
			$cookies.remove('__USER');
			$cookies.remove('__MENUS');
			$locstor.remove('__SIDEBAR_MENUS');
			$window.location.href="#/login";
			api.POST('logout',function success(response){
					$rootScope.$emit('UserLoggedOut');
			});
		}
		if($rootScope.__USER){
			$window.location.href="#/";
		}
		$scope.cancel = function(){
			$scope.loginMessage = null;
			$scope.User = {};
		}
		$scope.cancel();
		$scope.login = function(){
			var data = $scope.User;
			$scope.LoggingIn = true;
			$scope.loginMessage =null;
			isModuleListRequested = false;
			api.POST('login',data,function(response){
				$scope.LoggingIn = false;
				if(response.data.user){
					$rootScope.__USER = response.data;
					$cookies.put('__USER',JSON.stringify(response.data));
					$rootScope.__LOGGEDIN  = true;
					$rootScope.$emit('UserLoggedIn');
					$window.location.href="#/";
				}else{
					$scope.loginMessage = response.message;
				}
			});
		}
		
		$rootScope.$on('UserLoggedIn',function(evt){
			if(!isModuleListRequested)
				requestModulesList('userLoggedIn');
			isModuleListRequested=true;
			
		});
		
		$rootScope.$on('$routeChangeStart', function (scope, next, current) {
			if($rootScope.__LOGGEDIN)
				if(!$rootScope.__SIDEBAR_MENUS)
					readModuleListCache();
				else if(!isModuleListRequested)
					requestModulesList('routeStart');
			isModuleListRequested=true;
		});
		
		function requestModulesList(source){
			console.log(source,isModuleListRequested);
			if(isModuleListRequested) return;
			api.GET('modules',{limit:'less'},function(response){
				var modules = response.data;
				var menus = [];
				var lastIndex=-1;
				for(var i in modules){
					var mod =  modules[i];
					if(!$rootScope.__USER) break;
					var granted = $rootScope.__USER.user.access.indexOf(mod.id)!==-1 || mod.is_parent;
					if(!mod.is_child){
						//Menu
						if(mod.is_parent)
							mod.children=[];
						if(granted) menus.push(mod);
						lastIndex++;
					}else{
						//Submenu
						if(granted){
							menus[lastIndex].children.push(mod);
						}
							
							
					}
				}
				$locstor.set('__SIDEBAR_MENUS',JSON.stringify(menus));
				readModuleListCache();
			});
			
			
		}
		function readModuleListCache(){
				var sidebar = $locstor.get('__SIDEBAR_MENUS');
				var cache = JSON.parse(sidebar);
				$rootScope.__SIDEBAR_MENUS =  cache;
				
		}
	}]);
});
{'fraction_non_alphanumeric': 0.18072289156626506, 'fraction_numerical': 0.0006258801439524331, 'mean_word_length': 3.608507570295602, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3720138', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2141, 'n_words': 273}