'use strict';angular.module('twoBookApp').component('login',{templateUrl:'authentication/login/login.template.html',controller:['$scope','$state','LoginFactory','SessionService','$http','$element',function LoginController($scope,$state,LoginFactory,SessionService,$http,$element){$scope.loginForm={username:undefined,password:undefined,errorMessage:undefined};$scope.login=function(){kendo.ui.progress($element,true);LoginFactory($scope.loginForm.username,$scope.loginForm.password).then(function(response){SessionService.setToken(response.access_token) SessionService.setRefreshToken(response.refresh_token) $http({method:'GET',url:SessionService.getApiUrl()+'/api/clients',headers:{'Content-Type':'text/xml'}}).then(function(response){SessionService.setClients(response.data) $http({method:'GET',url:SessionService.getApiUrl()+'/api/bookkeepings',headers:{'Content-Type':'text/xml'}}).then(function(response){SessionService.setBookKeepings(response.data);DEBUG&&console.log('setCurrentBookkeeping from login with:'+response.data[0].Lea+'/'+response.data[0].Year) SessionService.setCurrentBookKeeping(response.data[0].Lea,response.data[0].Year);$http({method:'GET',url:SessionService.getApiUrl()+'/api/Account/UserInfo',headers:{'Content-Type':'text/xml'}}).then(function(response){SessionService.setUserInfo(response.data,true);SessionService.getSettings().then(function(response){kendo.ui.progress($element,false);$state.go('tbo.dashboard');},function(response){kendo.ui.progress($element,false);});},function(response){kendo.ui.progress($element,false);});},function(response){kendo.ui.progress($element,false);});},function(response){kendo.ui.progress($element,false);});},function(response){kendo.ui.progress($element,false);$scope.loginForm.errorMessage=response.error_description;});}}]});angular.module('twoBookApp').factory('LoginFactory',['$http','$q','SessionService','RequestsErrorHandler',function($http,$q,SessionService,RequestsErrorHandler){return function(username,password){var result=$q.defer();var params={grant_type:"password",userName:username,password:password};RequestsErrorHandler.specificallyHandled(function(){$http({method:'POST',url:SessionService.getApiUrl()+'/token',transformRequest:function(obj){var str=[];for(var p in obj) str.push(encodeURIComponent(p)+"="+encodeURIComponent(obj[p]));return str.join("&");},data:params,headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8;'}}).success(function(response){result.resolve(response);}).error(function(response){result.reject(response);});});return result.promise;}}]);['$state',function LogoutController($state){Session.clear();$state.go('landingpage');}];