soter/soter/static/app/shared/login.controller.js

41 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-06-11 12:31:33 +00:00
(function () {
'use strict';
angular.module('soter')
.controller('LoginController', ['$scope', '$rootScope', '$location', '$routeParams', 'AuthService', 'AUTH_EVENTS', 'Session', LoginController])
.controller('LogoutController', ['$scope', '$rootScope', '$location', 'AuthService', 'AUTH_EVENTS', 'Session', LogoutController]);
2015-06-11 12:31:33 +00:00
function LoginController($scope, $rootScope, $location, $routeParams, AuthService, AUTH_EVENTS, Session) {
2015-06-11 12:31:33 +00:00
$scope.submit = function () {
var data = {username: $scope.username, password: $scope.password};
AuthService.login(data).success(function (data) {
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
var came_from = $routeParams.came_from;
if (angular.isUndefined(came_from) || came_from === "" || came_from === "/login" || came_from === "/logout") {
came_from = "/";
}
$rootScope.auth = Session.data();
$location.url(came_from);
2015-06-11 12:31:33 +00:00
}).error(function (data, one, two, three) {
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
2015-06-11 12:31:33 +00:00
$scope.toasts.push({Type: 'Danger', Message: data});
});
2015-06-11 12:31:33 +00:00
};
$('#username').focus();
}
2015-06-11 12:31:33 +00:00
function LogoutController($scope, $rootScope, $location, AuthService, AUTH_EVENTS) {
AuthService.logout.
2015-06-11 12:31:33 +00:00
success(function () {
$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess);
2015-06-11 12:31:33 +00:00
$scope.toasts.push({Type: 'Success', Message: 'Logged out'});
$rootScope.auth = {};
2015-06-11 12:31:33 +00:00
$location.path('/');
}).
error(function (errorMessage) {
$scope.toasts.push({Type: 'Danger', Message: errorMessage});
});
}
})();