Created
March 7, 2016 13:18
-
-
Save swallentin/8746356c1f6d21d3a819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
}; | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment