harbor/static/ng/resources/js/components/sign-in/sign-in.directive.js

92 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-05-01 19:46:50 +02:00
(function() {
'use strict';
angular
.module('harbor.sign.in')
.directive('signIn', signIn);
SignInController.$inject = ['SignInService', 'LogOutService', 'currentUser', 'I18nService', '$window', '$scope'];
function SignInController(SignInService, LogOutService, currentUser, I18nService, $window, $scope) {
2016-05-01 19:46:50 +02:00
var vm = this;
2016-05-14 07:34:54 +02:00
vm.hasError = false;
vm.errorMessage = '';
vm.reset = reset;
2016-05-01 19:46:50 +02:00
vm.doSignIn = doSignIn;
vm.doSignUp = doSignUp;
vm.doForgotPassword = doForgotPassword;
2016-05-14 07:34:54 +02:00
vm.doContinue = doContinue;
vm.doLogOut = doLogOut;
2016-05-14 07:34:54 +02:00
function reset() {
vm.hasError = false;
vm.errorMessage = '';
}
function doSignIn(user) {
if(user && angular.isDefined(user.principal) && angular.isDefined(user.password)) {
SignInService(user.principal, user.password)
2016-05-01 19:46:50 +02:00
.success(signedInSuccess)
.error(signedInFailed);
}
}
function signedInSuccess(data, status) {
$window.location.href = "/ng/dashboard";
2016-05-01 19:46:50 +02:00
}
function signedInFailed(data, status) {
2016-05-14 07:34:54 +02:00
if(status === 401) {
vm.hasError = true;
vm.errorMessage = 'username_or_password_is_incorrect';
}
console.log('Failed sign in:' + data + ', status:' + status);
2016-05-01 19:46:50 +02:00
}
function doSignUp() {
$window.location.href = '/ng/sign_up';
}
function doForgotPassword() {
$window.location.href = '/ng/forgot_password';
}
function doContinue() {
$window.location.href = '/ng/dashboard';
}
function doLogOut() {
LogOutService()
.success(logOutSuccess)
.error(logOutFailed);
}
function logOutSuccess(data, status) {
currentUser.unset();
I18nService().unset();
$window.location.href= '/ng';
}
function logOutFailed(data, status) {
console.log('Failed to log out:' + data);
}
2016-05-01 19:46:50 +02:00
}
function signIn() {
var directive = {
'restrict': 'E',
'templateUrl': '/ng/sign_in',
'scope': true,
2016-05-01 19:46:50 +02:00
'controller': SignInController,
'controllerAs': 'vm',
'bindToController': true
2016-05-23 12:29:17 +02:00
};
2016-05-01 19:46:50 +02:00
return directive;
2016-05-14 07:34:54 +02:00
2016-05-01 19:46:50 +02:00
}
})();