tellform/public/modules/users/controllers/authentication.client.controller.js
2017-10-28 00:45:04 -07:00

61 lines
1.6 KiB
JavaScript
Executable file

'use strict';
angular.module('users').controller('AuthenticationController', ['$scope', '$location', '$state', '$rootScope', 'User', 'Auth', '$translate', '$window',
function($scope, $location, $state, $rootScope, User, Auth, $translate, $window) {
$scope = $rootScope;
$scope.credentials = {};
$scope.error = '';
$scope.forms = {};
$scope.signin = function() {
if(!$scope.forms.signinForm.$invalid){
User.login($scope.credentials).then(
function(response) {
Auth.login(response);
$scope.user = $rootScope.user = Auth.ensureHasCurrentUser(User);
if($state.previous.name !== 'home' && $state.previous.name !== 'verify' && $state.previous.name !== '') {
$state.go($state.previous.name);
} else {
$state.go('listForms');
}
},
function(error) {
$rootScope.user = Auth.ensureHasCurrentUser(User);
$scope.user = $rootScope.user;
$scope.error = error;
console.error('loginError: '+error);
}
);
}
};
$scope.signup = function() {
if($scope.credentials === 'admin'){
$scope.error = 'Username cannot be \'admin\'. Please pick another username.';
return;
}
if(!$scope.forms.signupForm.$invalid){
User.signup($scope.credentials).then(
function(response) {
$state.go('signup-success');
},
function(error) {
console.error(error);
if(error) {
$scope.error = error;
console.error(error);
} else {
console.error('No response received');
}
}
);
}
};
}
]);