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

41 lines
1.8 KiB
JavaScript

(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]);
function LoginController($scope, $rootScope, $location, $routeParams, AuthService, AUTH_EVENTS, Session) {
$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);
}).error(function (data, one, two, three) {
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
$scope.toasts.push({Type: 'Danger', Message: data});
});
};
$('#username').focus();
}
function LogoutController($scope, $rootScope, $location, AuthService, AUTH_EVENTS) {
AuthService.logout.
success(function () {
$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess);
$scope.toasts.push({Type: 'Success', Message: 'Logged out'});
$rootScope.auth = {};
$location.path('/');
}).
error(function (errorMessage) {
$scope.toasts.push({Type: 'Danger', Message: errorMessage});
});
}
})();