This commit is contained in:
kevgliss
2016-07-04 13:03:46 -07:00
committed by GitHub
parent 300e2d0b7d
commit c8eca56690
4 changed files with 56 additions and 42 deletions

View File

@ -5,12 +5,48 @@ angular.module('lemur')
$stateProvider.state('login', {
url: '/login',
templateUrl: '/angular/authentication/login/login.tpl.html',
controller: 'LoginController'
controller: 'LoginController',
params: {
'toState': 'certificates',
'toParams': {}
}
});
})
.controller('LoginController', function ($rootScope, $scope, AuthenticationService, UserService, providers) {
$scope.login = AuthenticationService.login;
$scope.authenticate = AuthenticationService.authenticate;
.controller('LoginController', function ($rootScope, $scope, $state, $auth, AuthenticationService, UserService, providers, toaster) {
$scope.login = function (username, password) {
return AuthenticationService.login(username, password).then(
function (user) {
$auth.setToken(user.token, true);
$rootScope.$emit('user:login');
$state.go($state.params.toState, $state.params.toParams);
},
function (response) {
toaster.pop({
type: 'error',
title: 'Whoa there',
body: response.data.message,
showCloseButton: true
});
});
};
$scope.authenticate = function (provider) {
return AuthenticationService.authenticate(provider).then(
function (user) {
$auth.setToken(user.token, true);
$rootScope.$emit('user:login');
$state.go($state.params.toState, $state.params.toParams);
},
function (response) {
toaster.pop({
type: 'error',
title: 'Whoa there',
body: response.data.message,
showCloseButton: true
});
});
};
$scope.logout = AuthenticationService.logout;
$scope.providers = providers;
@ -28,4 +64,4 @@ angular.module('lemur')
$rootScope.$on('user:logout', function () {
$scope.currentUser = null;
});
});
});

View File

@ -7,40 +7,11 @@ angular.module('lemur')
var AuthenticationService = this;
AuthenticationService.login = function (username, password) {
AuthenticationApi.customPOST({'username': username, 'password': password}, 'login')
.then(
function (user) {
$auth.setToken(user.token, true);
$rootScope.$emit('user:login');
$location.url('/certificates');
},
function (response) {
toaster.pop({
type: 'error',
title: 'Whoa there',
body: response.data.message,
showCloseButton: true
});
}
);
return AuthenticationApi.customPOST({'username': username, 'password': password}, 'login');
};
AuthenticationService.authenticate = function (provider) {
$auth.authenticate(provider)
.then(
function () {
UserService.getCurrentUser();
$rootScope.$emit('user:login');
$location.url('/certificates');
},
function (response) {
toaster.pop({
type: 'error',
title: 'Something went wrong',
body: response.data.message
});
}
);
return $auth.authenticate(provider);
};
AuthenticationService.logout = function () {
@ -55,8 +26,7 @@ angular.module('lemur')
title: 'Good job!',
body: 'You have been successfully logged out.'
});
$location.path('/');
$location.path('/login');
});
};
});
});