(function () { 'use strict'; angular .module('eva2-angular') .directive('evaInvalidCredentialsValidator', function ($rootScope) { return { require: 'ngModel', link: function (scope, el, attr, ctrl) { var onInvalidCredentials = function (event, message) { ctrl.$setValidity('invalidCredentials', false); ctrl.invalidCredentialsError = message.data.message; }; var onDestroy = function () { invalidCredentialsHandler(); }; var invalidCredentialsHandler = $rootScope.$on('authentication:login-failed', onInvalidCredentials); ctrl.$validators.invalidCredentials = function (value) { return true; }; scope.$on('$destroy', onDestroy); } }; }); angular .module('eva2-angular') .controller('LoginCtrl', LoginCtrl); LoginCtrl.$inject = ['$scope', '$state', 'ApiAuthentication', 'CurrentUser']; function LoginCtrl($scope, $state, ApiAuthentication, CurrentUser) { var handleSuccessAndRedirect = function () { $scope.error = false; if (CurrentUser && CurrentUser.domains && CurrentUser.domains.length === 1) { // If there's only one, we pick that, otherwise go to assignment switcher $state.go('assignment.jobs', { domain: CurrentUser.domains[0] }); } else { $state.go('assignments'); } }; var handleError = function () { $scope.error = true; }; var setLoadingToFalse = function () { $scope.loading = false; }; $scope.login = function () { $scope.loading = true; ApiAuthentication.login($scope.login.username, $scope.login.password) .then(handleSuccessAndRedirect) .catch(handleError) .finally(setLoadingToFalse); }; } })();