2014-02-10 13:09:31 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-09-02 23:21:24 -04:00
|
|
|
angular.module('users').controller('AuthenticationController', ['$scope', '$state', '$http', '$location', '$window', 'Authentication', 'PasswordValidator',
|
|
|
|
|
function ($scope, $state, $http, $location, $window, Authentication, PasswordValidator) {
|
2015-07-25 16:53:11 -04:00
|
|
|
$scope.authentication = Authentication;
|
2015-09-02 23:21:24 -04:00
|
|
|
$scope.popoverMsg = PasswordValidator.getPopoverMsg();
|
2015-07-25 16:53:11 -04:00
|
|
|
|
|
|
|
|
// Get an eventual error defined in the URL query string:
|
|
|
|
|
$scope.error = $location.search().err;
|
|
|
|
|
|
|
|
|
|
// If user is signed in then redirect back home
|
|
|
|
|
if ($scope.authentication.user) {
|
|
|
|
|
$location.path('/');
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
$scope.signup = function (isValid) {
|
|
|
|
|
$scope.error = null;
|
|
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
$scope.$broadcast('show-errors-check-validity', 'userForm');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
$http.post('/api/auth/signup', $scope.credentials).success(function (response) {
|
|
|
|
|
// If successful we assign the response to the global user model
|
|
|
|
|
$scope.authentication.user = response;
|
|
|
|
|
|
|
|
|
|
// And redirect to the previous or home page
|
|
|
|
|
$state.go($state.previous.state.name || 'home', $state.previous.params);
|
|
|
|
|
}).error(function (response) {
|
|
|
|
|
$scope.error = response.message;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
$scope.signin = function (isValid) {
|
|
|
|
|
$scope.error = null;
|
|
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
$scope.$broadcast('show-errors-check-validity', 'userForm');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
$http.post('/api/auth/signin', $scope.credentials).success(function (response) {
|
|
|
|
|
// If successful we assign the response to the global user model
|
|
|
|
|
$scope.authentication.user = response;
|
|
|
|
|
|
|
|
|
|
// And redirect to the previous or home page
|
|
|
|
|
$state.go($state.previous.state.name || 'home', $state.previous.params);
|
|
|
|
|
}).error(function (response) {
|
|
|
|
|
$scope.error = response.message;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// OAuth provider request
|
|
|
|
|
$scope.callOauthProvider = function (url) {
|
2015-08-23 03:10:58 -04:00
|
|
|
if ($state.previous && $state.previous.href) {
|
|
|
|
|
url += '?redirect_to=' + encodeURIComponent($state.previous.href);
|
2015-07-25 16:53:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Effectively call OAuth authentication route:
|
2015-08-23 03:10:58 -04:00
|
|
|
$window.location.href = url;
|
2015-07-25 16:53:11 -04:00
|
|
|
};
|
|
|
|
|
}
|
2015-02-16 21:35:33 +01:00
|
|
|
]);
|